How is it possible that Filter is applied when its dispatcher is FORWARD as well as when dispatcher is REQUEST?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 05:50:57

问题


I have a simple Filter:

public class TestFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("before");
        chain.doFilter(request, response);
        System.out.println("after");
    }

    public void destroy() {
    }

}

It's the first filter in web.xml and it has one of these two mappings:

<filter-mapping>
    <filter-name>cookie-test-filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

or

<filter-mapping>
    <filter-name>cookie-test-filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

In both cases I see the output:

before
before
after
after

(I've also tried INCLUDE as dispatcher just to be sure that everything works - there's no output with INCLUDE).

There're 3rd-party filters and servlets after this filter and I wonder: what should they do to make my filter applied in both described cases?


回答1:


Try using a single <filter-mapping> entry with both REQUEST and FORWARD dispatcher

Example

<filter-mapping>
    <filter-name>cookie-test-filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

Hope this clears up the issue you were having.



来源:https://stackoverflow.com/questions/5325405/how-is-it-possible-that-filter-is-applied-when-its-dispatcher-is-forward-as-well

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