Servlet Filter url-mapping /* is not working on 404 errors

岁酱吖の 提交于 2020-01-22 01:50:07

问题


I'm using Resin Server & Apache 2.2 with virtual hosting. Here I'm facing a big challenge in calling a concrete filter. I'm having a generic Filter class to process all the incoming request.

Ex: www.example.com/hello this hello is not calling the below filter instead it throwing file not found error(404).

If "hello" is having a proper servlet mapping then the below filter is working.

Web.xml :

<filter>
  <filter-name>CorpFilter</filter-name>
  <filter-class>com.filter.CorpFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CorpFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Apache Log :

[Fri Jan 04 22:05:07 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/xxxx/public_html/hello

Why the servlet filter is not being called and throwing 404 error? Servlet filter is initializing properly.

Thanks,


回答1:


Filters are by default dispatched on successful requests. They are by default not dispatched on erroneous requests. In order to dispatch them on erroneous requests as well, expand the filter mapping with the appropriate <dispatcher> elements:

<filter-mapping>
    <filter-name>CorpFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

Note that when specifying custom dispatcher types and you'd like to keep the default REQUEST dispatcher, then you should be explicitly specifying it as well. Note that I also assume that the 404 is not handled by the web proxy (Apache HTTPD), but by the servlet container (Resin) itself, for obvious reasons.



来源:https://stackoverflow.com/questions/14181731/servlet-filter-url-mapping-is-not-working-on-404-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!