Forums Register Login

Declaring and Ordering filters

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
All filters matching the <url-pattern> are taken first
All filters matching the <servlet-name> are taken next
+Pie Number of slices to send: Send
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??
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Thanks madhup.
the rule you posted works and is very easy to understand. But what about /HopsList.do(no.4)?
+Pie Number of slices to send: Send
Think about it again and you should get it !!!
+Pie Number of slices to send: Send
is that so, because /HopsList.do matches only Filter5 <url-pattern>??
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 838 times.
Similar Threads
Render my web page in pdf instead that hmtl
Filter Mapping Problem
Problem with method
redirect to error page upon session timedout
Optimizing Session tracking code.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:38:29.