• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

order of filter mapping

 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes yes it was my mistake.

Thanks Celinio Fernandes and Niranjan Deshpande.
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tahnks once again.
Now i am very clear about this topic.
 
Anitha Srinivasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sayak : that is a well put clarification indeed.
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot, even i had the same problem and i understood the real thing.

 
Leon Omk
Ranch Hand
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good!
 
Leon Omk
Ranch Hand
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic