Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring and Ordering filters

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
madhup narain
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All filters matching the <url-pattern> are taken first
All filters matching the <servlet-name> are taken next
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks madhup.
the rule you posted works and is very easy to understand. But what about /HopsList.do(no.4)?
 
madhup narain
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it again and you should get it !!!
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is that so, because /HopsList.do matches only Filter5 <url-pattern>??
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2264
76
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic