• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Filters

 
yekkala krishna
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know, why Filter1 would not be called. It has a matching URL pattern.
 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chinmaya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic