• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regarding filters and RequestDispather

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic