Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Filter Q in HF Book

 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question on one of the filter questions in HF book.
page: (680, 695)

<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>
<servlet-name>/Recipes/Modify/ModRecipes.do</servlet-name>
</filter-mapping>

<filter-mapping>
<filter-name>Filter5</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Shouldn't the element name in the 4th filter-mapping be <url-pattern>?

My answers to the exercise below these definitions to match the request paths in the correct sequence are:

1. /Recipes/HopsReport.do - Filters 1, 5
2. /Recipes/HopsList.do - Filters 1, 2, 5
3. /Recipes/Modify/ModRecipes.do - Filters 1, 4, 5
4. /HopsList.do - Filters 5
5. /Recipes/Add/AddRecipes.do - Filters 1, 3, 5


But the given answers in the book are:

1. - 1, 5
2. - 1, 5, 2
3. - 1, 5, 4
4. - 5
5. - 1, 3, 5

Can Somebody explain to me why the order of the filters is different?
 
Vengan Krish
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the Container's rules for ordering filters in Page 678.

1.Filters with matching URL patterns are placed in the chain in the order they are declared.

2.Next Filters with matching <servlet-name> are placed in the order they are in DD.

Hope this helps.
This is really a huge 'gotcha'.

Regards
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Vengan,
I went thru the rules. Still cannot make it out.


2 rules have been mentioned in p:678

a. All filters match the url-pattern are placed in the filter chain in the order they are declared in the DD.

b. After this, all filters that have a matching filter-name are placed in the filter chain for execution.

I also checked the errata site, couldn't find anything there.

Would appreciate if you can explain it more.

1. How can a <servlet-name> tag in a filter definition contain an url-pattern? Is it not supposed to contain the name of the servlet to which the filter applies?

2. The url-pattern /* (the last one) is defined as the last. So this filter would be included in every request but only as the LAST!. Isn't it?
 
tiim fei
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess this is because the mechanism of url-pattern match

I remember that ,container will match the url for longest possible matching
first, then try to match the .suffix pattern.

If this is true ,then the answer could be explained.
First a path Recipes/HopsList.do
the container go through the filters for Recipes/HopsList
find 1, 5, maching(The order is in dd)
then for suffix mapping , find 2 .

But this is my own assumption. Hope somebody to clarify this .
 
trivikram Kumar
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pals,

First the container keeps in Q the filters with the matching URL pattern in the order in which they appear in the DD.Then the order is followed by the filters with the matching servlet name...Even I was confused at first but after reading carefully the white text in black box there in HFS,....I could come to a conclusion.

Thanks,
Javainn
 
Vengan Krish
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishwa,

1. How can a <servlet-name> tag in a filter definition contain an url-pattern? Is it not supposed to contain the name of the servlet to which the filter applies?
Your initial posting has a mistake. Filter 2 mapping has <servlet-name> in the HFS book, not <url-pattern>.

2. The url-pattern /* (the last one) is defined as the last. So this filter would be included in every request but only as the LAST!. Isn't it?
No.

Here is the filter spec in the DD in a matrix.

DD <url-pattern> DD <servlet-name>

Filter 1 /Recipes/* -
Filter 2 - /Recipes/HopsList.do
Filter 3 /Recipes/Add/* -
Filter 4 - /Recipes/Modify/ModRecipes.do
Filter 5 /* -


Let's say we get a request URL /Recipes/HopsList.do.

Applying rule 1, Container goes thro' the <url-pattern> column looking for match. Filter 1 and 5 match.

Applying rule 2, Container goes thro' the <servlet-name> column. Filter 2 match.

So the order is 1 5 2. Isn't it straightforward ?

Makes sense?
 
Vengan Krish
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My matrix in the above posting is messed up.

DD <url-pattern> DD <servlet-name>

Filter 1 /Recipes/* -
Filter 2 - /Recipes/HopsList.do
Filter 3 /Recipes/Add/* -
Filter 4 - /Recipes/Modify/ModRecipes.do
Filter 5 /* -
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tiim,
Even if I assume the longest possible path comes first, then also the url-pattern /* (the last one), should come at the end, isn't it?.

HF book in its Container rules(p:80) for filters clearly says that the algorithm is different. It doesn't pick up the longest possible path match, but picks up all of the url-patterns that match, place them in the filter chain and execute them one by one, in the order they are defined in the DD.

I am still baffled.
 
Vengan Krish
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't get it any better - sorry.
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vengan Krish:
I can't get it any better - sorry.


Thanks very much Vengan,
I missed your posts. We seemed to have posted at the same time.
I think I finally understood it. Very tricky indeed.
[ May 27, 2005: Message edited by: Vishwa Kumba ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic