Servlet Filter - forwarded request from a servlet

2020-06-17 06:11发布

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.

1条回答
家丑人穷心不美
2楼-- · 2020-06-17 06:42

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.

查看更多
登录 后发表回答