File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Declaring and Ordering filters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Declaring and Ordering filters" Watch "Declaring and Ordering filters" New topic
Author

Declaring and Ordering filters

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Can someone explain how filters are mapped to a URL pattern and/or servlet name?
I read this topic in HEAD FIRST SERVLET & JSP, but still its not clear.
Do provide examples for better understanding.


kunal
madhup narain
Ranch Hand

Joined: Dec 14, 2004
Posts: 148
All filters matching the <url-pattern> are taken first
All filters matching the <servlet-name> are taken next


Money for nothing and Java for Free
SCJP, SCWCD
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Head First sevlet & Jsp ch-13, Filters & Wrappers, pg-712 has a Question based on filters.

Assume Filter1 through Filter5 have been properly declared, and that the servlet names are the same as their mappings.



Request paths are given Answers
1) /Recipies/HopsReport.do Filter Sequence: 1,5
2)/Recipies/HopsList.do Filter Sequence: 1,5,2
3)/Recipies/Modify/ModRecipies.do Filter Sequence: 1,5,4
4)/HopsList.do Filter Sequence: 5
5)/Recipies/Add/AddReceipts.do Filter Sequence: 1,3,5


My question is that why in no.2) above, filter sequence is 1,5,2 and not 1,2,5. Similar doubt in no.3)? why no.4) above only evaluates to filter5, why not filter2 too??
madhup narain
Ranch Hand

Joined: Dec 14, 2004
Posts: 148
Taking /Recipies/HopsList.do as an example

Rule 1: Firstly take all the <url-patterns>

remember the key <URL-PATTERNS> ; therefore we get the following


That means Filter1 -> Filter5

Rule 2: Take all the <servlet-name> matches


Thats the only one that qualifies therefore Filter1 -> Filter5 ->Filter2

Apply the same rule to the rest of the filters
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks madhup.
the rule you posted works and is very easy to understand. But what about /HopsList.do(no.4)?
madhup narain
Ranch Hand

Joined: Dec 14, 2004
Posts: 148
Think about it again and you should get it !!!
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
is that so, because /HopsList.do matches only Filter5 <url-pattern>??
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1665
    
  25

Hi Kunal,
because /HopsList.do matches only Filter5 <url-pattern>??

Yes, the Filter with an url pattern of "/*" is applied to all the servlets and static content pages in the Web application, because every request URI matches the "/*" URL pattern.

Regards,
Frits
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

Also think of it this way : if EVERY filter applies (which in this exact example is impossible), the order would be

Filter 1 -> Filter 3 -> Filter 5 -> Filter 2 -> Filter 4

So you basically need to sort filter according to two parameters, first by whether it is url-pattern or servlet-name, url-pattern coming first. And second by the order of declaration in DD file.

I hope this helped.


Personal page, SCJP 6 with 91%, SCWCD 5 with 84%, OCMJD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declaring and Ordering filters