This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Regarding Filters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Regarding Filters" Watch "Regarding Filters" New topic
Author

Regarding Filters

yekkala krishna
Ranch Hand

Joined: Mar 04, 2007
Posts: 105
Hi,

I am following Hanumant Deshmukh material for preparation of SCWCD Exam.I have small query regarding the following question.Can Anybody expalain the below question?

Question: 3. Given these filter mapping declarations:
<filter-mapping>
<filter-name>FilterOne</filter-name>
<url-pattern>/admin/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>FilterTwo</filter-name>
<url-pattern>/users/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>FilterThree</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>FilterTwo</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


in what order are the filters invoked for the following browser request?
/admin/index.jsp

a FilterOne, FilterThree
b FilterOne, FilterTwo, FilterThree
c FilterThree, FilterTwo,FilterOne
d FilterThree, FilterTwo
e FilterThree
f None of these filters are invoked.

Answer for above question is d.

Can any body explain how FilterTwo invokes after invocation of FilterThree.


With Regards,
Rama Krishna.Y
Patil Niteen
Ranch Hand

Joined: Dec 10, 2009
Posts: 48

The rules for filter mapping are:

1. All filters with matching url patterns are located first.Filters with matching url patterns are placed in the chain in the order in which they are declared in the DD.
2. Once the filters with the matching url patterns are placed in the chain, the container does the same thing with the filters that have a matching <servlet-name> in the DD.

So,
FilterTwo invokes after invocation of FilterThree.


Thanks.


SCJP 1.4, SCWCD 5
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
I would like to know, why Filter1 would not be called. It has a matching URL pattern.


Thanks and Regards
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Satya.

If we don't mention <dispatcher> element in <filter-mapping>, or mention it as empty like then it defaults to REQUEST.

Suppose if we send request to the container like, , then container selects the appropriate filters by seeing its <url-parttern> and <dispatcher> element. Here first it sees the 'FilterOne' url-pattern(/admin/*), this is ok, and it sees its <dispatcher> entry, here it is 'FORWARD', it skip this step and moves to the next ones, 'FilterThree' and 'FilterTwo', they satisfy the rules. Finally, it makes a mapping that first I(container) should call 'FilterThree' and after I should call 'FilterTwo'.



Incase if we use RequestDispatcher to forward request to any resource like calling,

then before forwarding to the resource that was present in 'admin' directory, first it checks the <filter-mapping>'s <dispatcher> declaration, like is there is 'FORWARD' present in it? Here it is present. Then it calls the 'FilterOne's doFilter() method and if our doFilter() contains then the resource will be forward to the 'result.jsp'.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Thanks Chinmaya
 
Don't get me started about those stupid light bulbs.
 
subject: Regarding Filters