servlet 链过滤器

发布于 2024-12-28 19:58:32 字数 1417 浏览 1 评论 0原文

如何编写一个过滤器类以将响应连同 GET 参数一起从一个 servlet 传递到另一个 servlet?

这是我尝试过的概述(我从这个问题< /a>)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TranslateFilter implements Filter {

  private FilterConfig config = null;

  public void init(FilterConfig config) throws ServletException {
    this.config = config;
  }

  public void destroy() {
    config = null;
  }

  public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain) throws IOException, ServletException {

    chain.doFilter(request, response);
    ..

    RequestDispatcher dispatch = request.getRequestDispatcher("/Translate");
    dispatch.forward(request, response); 
    ..
  }
}

和 web.xml 中的

<servlet-mapping>
    <servlet-name>process</servlet-name>
    <url-pattern>/Process
</servlet-mapping>

<servlet-mapping>
    <servlet-name>translate</servlet-name>
    <url-pattern>/Translate
</servlet-mapping>

<filter-mapping>
    <filter-name>processChain</filter-name>
    <servlet-name>process</servlet-name>
</filter-mapping>

但它不起作用。它不会转发到第二个 servlet。我没有调试环境设置,所以我无法判断它在哪里失败,但有人可以指出我正确的方向吗?

How can I write a filter class to pass the response from one servlet to another along with the GET parameters?

This is the outline of what I've tried (I got most of it from this question)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TranslateFilter implements Filter {

  private FilterConfig config = null;

  public void init(FilterConfig config) throws ServletException {
    this.config = config;
  }

  public void destroy() {
    config = null;
  }

  public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain) throws IOException, ServletException {

    chain.doFilter(request, response);
    ..

    RequestDispatcher dispatch = request.getRequestDispatcher("/Translate");
    dispatch.forward(request, response); 
    ..
  }
}

and this in the web.xml

<servlet-mapping>
    <servlet-name>process</servlet-name>
    <url-pattern>/Process
</servlet-mapping>

<servlet-mapping>
    <servlet-name>translate</servlet-name>
    <url-pattern>/Translate
</servlet-mapping>

<filter-mapping>
    <filter-name>processChain</filter-name>
    <servlet-name>process</servlet-name>
</filter-mapping>

But it doesn't not work. It's not forwarding to the second servlet. I don't have a debugging environment setup so I can't tell where it's failing but can someone point me in the right direction?

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

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

发布评论

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

评论(1

荆棘i 2025-01-04 19:58:32

FilterChain#doFilter() 继续请求处理,并且仅在目标控制器完成其工作并且呈现并提交响应时才返回。

如果您的目的是通过 RequestDispatcher#forward() 将请求更改为不同的服务器端目标(或者当您想让客户端发送由 HttpServletResponse#sendRedirect() 发出的新请求)。您应该通过在服务器日志中查看IllegalStateException:响应已提交注意到这一点。

因此,要么删除它,以便您只得到转发,

request.getRequestDispatcher("/Translate").forward(request, response); 

要么将其设置

if (someCondition) {
    chain.doFilter(request, response);
} else {
    request.getRequestDispatcher("/Translate").forward(request, response); 
}

为与具体问题无关的条件,如果我理解/猜测您的实际功能需求是正确的,那么您会更寻找/process servlet 中的 RequestDispatcher#include()。另请参阅如何按顺序执行多个 servlet?

The FilterChain#doFilter() continues the request processing and only returns when the target controllers have finished their job and the response is been rendered and committed.

You should not invoke it if your intent is to change the request to a different server side destination by RequestDispatcher#forward() (or when you want to let the client side send a new request by HttpServletResponse#sendRedirect()). You should have noticed this by seeing IllegalStateException: response already committed in the server logs.

So, either remove it so that you only end up with the forward,

request.getRequestDispatcher("/Translate").forward(request, response); 

or make it a conditional

if (someCondition) {
    chain.doFilter(request, response);
} else {
    request.getRequestDispatcher("/Translate").forward(request, response); 
}

Unrelated to the concrete question, if I understand/guess your actual functional requirement right, you're more looking for the RequestDispatcher#include() in the /process servlet. See also How do I execute multiple servlets in sequence?

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