开发者

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

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开发者_JAVA技巧 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜