当 Filter 的调度程序为 FORWARD 以及当调度程序为 REQUEST 时,如何可能应用 Filter?

发布于 2024-10-22 03:16:48 字数 1203 浏览 2 评论 0原文

我有一个简单的过滤器:

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 中的第一个过滤器,它具有以下两个映射之一:

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

在这两种情况下我都看到输出:(

before
before
after
after

我也尝试使用 INCLUDE 作为调度程序确保一切正常 - 没有包含 INCLUDE 的输出)。

此过滤器之后还有第 3 方过滤器和 servlet,我想知道:他们应该做什么才能使我的过滤器应用于所描述的两种情况?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2024-10-29 03:16:48

尝试将单个 条目与 REQUESTFORWARD 调度程序

示例

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

一起使用希望这能解决问题你遇到的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文