Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet Filter - forwarded request from a servlet will go to servlet filter or not?

If any J2EE application hit servlet directly and then servlet forward the same request to some .jsp page.

request.getRequestDispatcher("Login.jsp").forward(request, response);

And I have a servlet filter with below url-pattern

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

So, will that forwarded request comes to filter also or not.

In my case it is not coming, is this a as expected behaviour. Just want to understand this.

like image 412
Simpal Kumar Avatar asked Jun 26 '15 05:06

Simpal Kumar


1 Answers

If you want the filter mapping to be invoked for forward requests, you have to put this in web.xml

This support is there since Servlet2.4

<filter-mapping>
  <filter-name>myfilter</filter-name>
  <url-pattern>/mypath/*</url-pattern>
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>REQUEST</dispatcher>
</filter-mapping>

The supported values for dispatcher are :

  • REQUEST: 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.

  • FORWARD: 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.

  • INCLUDE: 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.

  • ERROR: The request that is being processed with the error page mechanism specified in ”Error Handling” to an error resource matching the <url-pattern>. This is indicated by a <dispatcher> element with the value ERROR.

  • ASYNC: The request is being processed with the async context dispatch mechanism specified in ”Asynchronous processing” to a web component using a dispatch call. This is indicated by a <dispatcher> element with the value ASYNC.

like image 121
Ramesh PVK Avatar answered Nov 08 '22 18:11

Ramesh PVK