It's not a secret anymore!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Filter Chaning Query..! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Filter Chaning Query..!" Watch "Filter Chaning Query..!" New topic
Author

Filter Chaning Query..!

Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
Friends,

Can any one tell me the Filter ordering Sequence Fundamental. I did not understand It. I did not understand the Question and Answer Mentioned on Page 695 of hfsj.

Here's the Question :

<filter-mapping>
<filter-name>Filter1</filter-name>
<url-pattern>/Recipes/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter2</filter-name>
<servlet-name>/Recipes/HopsList.do</servlet-name>
</filter-mapping>


<filter-mapping>
<filter-name>Filter3</filter-name>
<url-pattern>/Recipes/Add/*</url-pattern>
</filter-mapping>


<filter-mapping>
<filter-name>Filter4</filter-name>
<url-pattern>/Recipes/Modify/ModRecipes.do</url-pattern>
</filter-mapping>


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


Request Path


1)/Recipes/HopsReport.do
2)/Recipes/HopsList.do
3)/Recipes/Modify/ModRecipes.do
4)/HopsList.do
5)/Recipes/Add/AddRecipes.do

Filter Sequence

1)1,5
2)1,5,2
3)1,5,4
4)5
5)1,3,5

Can Any Body Please Explain me in Detail the Sequencing on this Filters for all 5 request Paths?

Regards,
Nilesh


Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
KRK Gowda
Ranch Hand

Joined: Nov 02, 2004
Posts: 132
Hi Nilesh,
concept of order in which filters are invoked is clear in HFJS.
order of filters is based on two conditions :
first, container will consider filters in the order they are declared in DD.
second, container will put the filters that are declared with url pattern tag first into queue. then it will consider filters for specific match.
in the problem mentioned above, when container receive request for /Recipes/HopsReport.do, container will consider all filters whose url-patterns match with the requested resource. i.e. fillter 1 and filter 5. when container traverse through DD, it encounter filter 1 first. so filter 1 will be executed first.
consider, /Recipes/HopsList.do for this request, 3 filters do have match. filter 1, filter 2 and filter 5.
container will traverse through DD for matching url pattern and it finds filter 1 and filter 5. so these 2 are added into queue. againg container checks for specific match by comparing servlet name tag. it finds specific match with filter 2. filter 2 will be added to queue.
hence, the order in which filters are executed is filter 1, filter 5 and filter 2.

hope it will be clear now.
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
to make things simple, remember this -

filters are executed before the servlets
or to be more precise,
filters with <url-patterns> in their mapping are executed before those with a <servlet-name> in the mapping.


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
K Thapa
Greenhorn

Joined: Sep 02, 2004
Posts: 8
I am a little confused here. Is Filter Mapping mechanism different than the servlet mapping ?

Once it finds all the filters matching the url-pattern first, shouldn't it go by the exact match first ( the longest directory mapping) instead of ordering in the DD?
In /Recipes/Modify/ModRecipes.do,
it finds Filters 1, 5 and 4.
Because 4 is the exact match, that should be called first and then 1 and then 5.
So the ordering should be 4,1,5 instead of 1,5,4

Any thoughts? or is it yet another notorious Sun discrepencies?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Filter Chaning Query..!
 
Similar Threads
Doubt in ordering of filters
Filter Q in HF Book
Filter sequence question in Head first book
Filter Mapping
[Solved][HFSJ] Filter Preferences