Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding filters and RequestDispather

 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you specify the <dispather> element in filter element with any of the three FORWARD,INCLUDE with <servlet-name> tag what will happen ???

whether the container will invoke the filter for a request for the specified servlet ? or when the forward() or include() call happened in the specified servlet ? or it will invoke on both occasions ?

for example

(from servlet spec 2.4)

<filter-mapping>
<filter-name>Logging Filter</filter-name>
<servlet-name>ProductServlet</servlet-name>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>

what will happen??
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Santhi Bharath:

what will happen?


why cant you try yourself? practise makes confident.

anyway, if you include <dispatcher>INCLUDE explicitly,then only the filter is called at the time of including the servlet from other source file(jsp,servlet,etc..)...not for a request(the default behaviour)
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by seetharaman venkatasamy:


why cant you try yourself? practise makes confident.

anyway, if you include <dispatcher>INCLUDE explicitly,then only the filter is called at the time of including the servlet from other source file(jsp,servlet,etc..)...not for a request(the default behaviour)


If you specify one value for <dispatched>, then REQUEST must also be specified. By the way, its clearly written in HFSJ.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandeep Bhandari:

If you specify one value for <dispatched>, then REQUEST must also be specified. By the way, its clearly written in HFSJ.


Not really .... if you mention FORWARD only. then filter will invoke at the time of forward call to the particular servlet...in other case no.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[code]
<filter>
<filter-name>TestFilter</filter-name>
<filter-class>com.example.TestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-patter>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter>

There are 4 types of dispatcher
By using the new <dispatcher> element in the deployment descriptor, the
developer can indicate for a filter-mapping whether he would like the filter to be
applied to requests when:
1. The request comes directly from the client.This is indicated by a <dispatcher> element with value REQUEST,or by the absence of any <dispatcher> elements.THIS IS DEFAULT behavior if no <dispatcher/> elements are specified.
2. The request is being processed under a request dispatcher representing the Web component matching the <url-pattern> or <servlet-name> using a forward() call. This is indicated by a <dispatcher> element with value FORWARD.
3. The request is being processed under a request dispatcher representing the Web component matching the <url-pattern> or <servlet-name> using an include()call.This is indicated by a <dispatcher> element with value INCLUDE.
4. The request is being processed with the error page mechanism specified in ´┐ŻError Handling´┐Ż on page 73 to an error resource matching the <url-pattern>. This is indicated by a <dispatcher> element with the value ERROR.
5. Or any combination of 1, 2, 3, or 4 above can be used together based on the requirement.
From 2.4 Servlet Specification.

TestFilter will now be invoked for all JSP in the web application if the request is from a client. If say X.jsp does a forward to Y.JSP then the TestFilter will not be invoked. If you want TestFilter to be invoked for Forward requests then you need to modify the <filter-mapping/> tag as follows

<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-patter>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter>

Also
<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-patter>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter>

Hope its clear.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deepak Jain:

<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-patter>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter>

Also
<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-patter>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter>



Excellent explanation Deepak
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic