当调度程序为FORWARD以及调度程序为REQUEST时,如何应用Filter? [英] How is it possible that Filter is applied when its dispatcher is FORWARD as well as when dispatcher is REQUEST?

查看:199
本文介绍了当调度程序为FORWARD以及调度程序为REQUEST时,如何应用Filter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的过滤器:

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() {
    }

}

这是web.xml中的第一个过滤器,它有以下两个映射之一:

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>

<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

(我还尝试 INCLUDE 作为调度员只是为了确保一切正常 - 没有输出 INCLUDE )。

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

这个过滤器之后有第三方过滤器和servlet,我想知道:他们应该做些什么来使我的过滤器应用于所描述的两种情况?

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?

推荐答案

尝试使用单个< filter-mapping> 条目同时请求 FORWARD 调度员

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

示例

<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.

这篇关于当调度程序为FORWARD以及调度程序为REQUEST时,如何应用Filter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆