| Author |
order of filter mapping
|
Sreeraj G Harilal
Ranch Hand
Joined: Apr 19, 2006
Posts: 310
|
|
In book Head First Servlet & JSP Page 695 (Sharpen your Pensil) Q) Write down the sequence in which the filters will be executed for the request path.Assume Filter1-Filter5 properly declared. <filter-mapping> <filter-name>Filter1</filter-name> <url-pattern>/Recipes/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Filter2</filter-name> <url-pattern>/Recipes/HopsList.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Filter3</filter-name> <url-pattern>/Recipes/Add/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Filter4</filter-name> <url-pattern>/Recipes/Modify/ModRecips.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Filter1</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> Request Path is /Recipes/HopsList.do The answer is Filter1,Filter5,Filter2
Here is my doubt In this book they said "Filters with matching URL patterns are placed in the chain in the order in which they are declared in the DD." If that so the answer must be Filter1,Filter2,Filter5
or
If the order of filter mapping is like servlet mapping then also the answer must be Filter1, Filter2, Filter5
because key rules about servlet mapping is 1) It look first for an exact match.If it can't find an exact match,it looks for a directory match.If it can't find a directory match,it looks for an extension match. 2) If a request matches more than one directory <url-pattern>, the container chooses the longest mapping.In other words, a request for /foo/bar/myStuff.do will map to the <url-pattern> /foo/bar/* even though it also matches the <url-pattern> /foo/*.The most specific match always wins. If i am wrong please clear me the order of filter mappings.
|
SCJP 5.0<br />SCWCD 1.4<br />Preparing for <b>SCEA</b>.<br /><b>"I prefer an interesting vice to a virtue that bores."</b>
|
 |
Celinio Fernandes
Ranch Hand
Joined: Jun 28, 2003
Posts: 546
|
|
That is not what the DD is made of. 2nd and 4th filter mappings are mapped to servlet names, not url patterns. That is why you think the answer is wrong.
|
SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
|
 |
Niranjan Deshpande
Ranch Hand
Joined: Oct 16, 2005
Posts: 1277
|
|
when the container recives a request, it first finds all the filter mappings with a <url-pattern> that matches the request URI. This becomes the first set of filters in the filter chain. Next it finds all the filter mappings with a <servlet-name> that matches the request URI. This becomes the second set of filters in the filter chain. in both the sets the filters are executed in the order in which they are declared in the D.D and most importantly, set 1 executed before set 2 hth
|
SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
|
 |
Niranjan Deshpande
Ranch Hand
Joined: Oct 16, 2005
Posts: 1277
|
|
when the container recives a request, it first finds all the filter mappings with a <url-pattern> that matches the request URI. This becomes the first set of filters in the filter chain. Next it finds all the filter mappings with a <servlet-name> that matches the request URI. This becomes the second set of filters in the filter chain. in both the sets the filters are executed in the order in which they are declared in the D.D and most importantly, set 1 executed before set 2 hth
|
 |
Niranjan Deshpande
Ranch Hand
Joined: Oct 16, 2005
Posts: 1277
|
|
when the container recives a request, it first finds all the filter mappings with a <url-pattern> that matches the request URI. This becomes the first set of filters in the filter chain. Next it finds all the filter mappings with a <servlet-name> that matches the request URI. This becomes the second set of filters in the filter chain. in both the sets the filters are executed in the order in which they are declared in the D.D and most importantly, set 1 executed before set 2 hth
|
 |
Sreeraj G Harilal
Ranch Hand
Joined: Apr 19, 2006
Posts: 310
|
|
yes yes it was my mistake. Thanks Celinio Fernandes and Niranjan Deshpande.
|
 |
Sreeraj G Harilal
Ranch Hand
Joined: Apr 19, 2006
Posts: 310
|
|
Tahnks once again. Now i am very clear about this topic.
|
 |
Anitha Srinivasan
Ranch Hand
Joined: Nov 07, 2006
Posts: 53
|
|
so the order of filter mapping and servlet mapping got different rules? I thought it was like servlet mapping. Thanks, Anitha
|
 |
Elan Ram
Ranch Hand
Joined: Dec 14, 2006
Posts: 40
|
|
Hi, According to the Servlet 2.4 Spec for Filters, 1st set - url pattern 2nd set - servlet name. But Tomcat/JBoss not implemented in this order.
|
Thanks and regards,
Elan
|
 |
Sayak Banerjee
Ranch Hand
Joined: Nov 28, 2006
Posts: 292
|
|
No Anitha...the mapping does not have different rules for servlet-mapping and filter-mapping...it's exactly the same....the point of discussion here was the order of the filters in the chain and that's exactly as Niranjan has described Well, Elan, FYI there are other cases with Tomcat as well where the spec. has not been strictly followed....so don't concentrate too much on Tomcat implementation bugs....Adhere to the spec....that's what matters the most [ December 14, 2006: Message edited by: Sayak Banerjee ]
|
Turn on, tune in, drop out.
|
 |
Sreeraj G Harilal
Ranch Hand
Joined: Apr 19, 2006
Posts: 310
|
|
No Sayak, you are mistaken. In servlet mapping, longest url pattern will execute first. But In filter mapping, it will execute the filter according to the order of filter mapping in the DD
|
 |
Sayak Banerjee
Ranch Hand
Joined: Nov 28, 2006
Posts: 292
|
|
Why did you dump my point ? Here's somethin' from Servlet Spec. 2.4 page 54
When processing a <filter-mapping> element using the <url-pattern> style, the container must determine whether the <url-pattern> matches the request URI using the path mapping rules defined in Chapter SRV.11, �Mapping Requests to Servlets�.
|
 |
Sayak Banerjee
Ranch Hand
Joined: Nov 28, 2006
Posts: 292
|
|
However, regarding ordering, I still maintain that whatever Niranjan has explained above is the way it should be...here's somethin' from the spec.
The order the container uses in building the chain of filters to be applied for a particular request URI is as follows: 1. First, the <url-pattern> matching filter mappings in the same order that these elements appear in the deployment descriptor. 2. Next, the <servlet-name> matching filter mappings in the same order that these elements appear in the deployment descriptor.
|
 |
Sreeraj G Harilal
Ranch Hand
Joined: Apr 19, 2006
Posts: 310
|
|
Sayak, execution of Servlets and Filters are different based on the order of servlet mapping and filter mapping respectively in DD. suppose the servet mapping is like here
<servlet-mapping> <servlet-name>RedServlet</servlet-name> <url-pattern>/red/*<url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>BlueServlet</servlet-name> <url-pattern>/red/red/*<url-pattern> </servlet-mapping>
http://localhost/red/red/* it will definitly execute BlueServlet only (that means first). logest mapping comes first Then check this
<filter-mapping> <filter-name>RedServletFilter</filter-name> <url-pattern>/red/*<url-pattern> </filter-mapping> <filter-mapping> <filter-name>BlueServletFilter</filter-name> <url-pattern>/red/red/*<url-pattern> </filter-mapping>
http://localhost/red/red/* First it will execute RedServletFilter, then BlueServletFilter (filter mapping Order in the DD)
|
 |
Sayak Banerjee
Ranch Hand
Joined: Nov 28, 2006
Posts: 292
|
|
I see what you mean....yeah, its true that in case of filter-mapping it does not choose a winner as it does for servlet-mapping....all the eligible filters for a particular url-pattern gets a place in the chain....but the basic rules used for mapping are still the same as in servlet-mapping...As a matter of fact, what you're sayin' is also there on the same page in the spec.
If there are filters using <url-pattern> matching and the <url-pattern> matches the request URI according to the rules of Section SRV.11.2, �Specification of Mappings�, the container builds the chain of <url-pattern> matched filters in the same order as declared in the deployment descriptor. The last filter in this chain is the last <url-pattern> matching filter in the deployment descriptor for this request URI.
Also, don't confuse ordering filters in the chain with mapping...there is no
(filter mapping Order in the DD)
 [ December 14, 2006: Message edited by: Sayak Banerjee ]
|
 |
Celinio Fernandes
Ranch Hand
Joined: Jun 28, 2003
Posts: 546
|
|
|
Sayak : that is a well put clarification indeed.
|
 |
Sreeraj G Harilal
Ranch Hand
Joined: Apr 19, 2006
Posts: 310
|
|
Sayak, we are saying the same thing. filter mapping order in the DD , what i mean is the same you
he container builds the chain of <url-pattern> matched filters in the same order as declared in the deployment descriptor.
|
 |
shukla raghav
Ranch Hand
Joined: Aug 03, 2008
Posts: 184
|
|
Thanks alot, even i had the same problem and i understood the real thing.
|
 |
Leon Omk
Ranch Hand
Joined: Aug 17, 2010
Posts: 72
|
|
|
Good!
|
OK, so that other guy knows Java better than I do, but I bet he can't speak Wuhanese(a Chinese Dialect) like me.
|
 |
Leon Omk
Ranch Hand
Joined: Aug 17, 2010
Posts: 72
|
|
Celinio Fernandes wrote:That is not what the DD is made of.
2nd and 4th filter mappings are mapped to servlet names, not url patterns.
That is why you think the answer is wrong.
helpful!
|
 |
Leon Omk
Ranch Hand
Joined: Aug 17, 2010
Posts: 72
|
|
BTW, The filter configuration from Head First Servlet and JSP is like this, note that <servlet-name> is actually used for filter 2, 4
<filter-mapping>
<filter-name>Filter1</filter-name>
<url-pattern>/Recipes/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Filter2</filter-name>
<servlet-name>/Recipes/HopsList.do</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Filter3</filter-name>
<url-pattern>/Recipes/Add/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Filter4</filter-name>
<servlet-name>/Recipes/Modify/ModRecipes.do</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Filter5</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
|
 |
 |
|
|
subject: order of filter mapping
|
|
|