• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unable to understand filter execution sequence

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a example on filter order execution in Servlets and JSP in Hed first Book

He tells us to write the order in which the filters will be executed. It is gives as bellow



The url pattern given is

/Recipes/HopsReport.do (according to me 1,5) (what actually is 1,5)
/Recipes/HopsList.do (according to me 1,2,5) (what actually is 1,5,2)
/Recipes/Modify/ModRecipes.do (according to me 1,4,5) (what actually is 1,5,4)
/HopsList.do (according to me 5) (what actually is 5)
/Recipes/Add/AddRecipes.do (according to me 1,3,5) (what actually is 1,3,5)

The order should be the order in which they have been declared. According to that for second one it should be 1,2,5 but in the book it is mentioned as 1,5,2. I don't get it. Filter5 is declared after Filter2. But still Filter5 get executed before Filter2. So is the case with all other urls. Where my understanding is wrong. please help me.
 
Ranch Hand
Posts: 463
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,

While building the filter chain servlet uses <url-pattern> matches first and then <servlet-name> matches later in the order they specified in the DD. That means if filters 1 2 3 matched with <url-pattern> these will be added to filter chain, and then if filter 4 and filter 5 matches with <servlet-name> these will be added later. So now filter chain has filters 1 2 3 4 5 in the order.

If <url-pattern> in DD is in the order filters 3 1 2 then the final order is 3 1 2 4 5.

Hope this helps.
 
Anil Deshpande
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I didn't get you. Take the example of second url.

/Recipes/HopsList.do

it first matches with Filter1 then Filter2 (since it is in that order before Filter5) and then finally Filter5. Thus according to me it should have been 1,2,5

But in the book it is mentioned as 1,5,2 How come it is able to match with Filter5 even though it is the last one to match because of the order in which it has been written.

Try to explain me using this url as an example.

 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,

As I said, the container has to build the filter chain to execute the filters. While building the filter chain the container uses the following rule.

- It adds all the filters matching <url-pattern> to filter chain in the order they're declared in DD.
- It adds all the filters matching <servlet-name> to filter chain in the order they're declared in DD.

In our example, filter 1 and filter 5 are matched based on <url-pattern>, then finally only one match found using <servlet-name> which is filter 2.

So the order of executing is 1 5 2.

Refer HFSJ Second Edition page no. 710. (I've PDF version)
 
Anil Deshpande
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I understood the point now. Thank you
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic