简体   繁体   English

servlet链过滤器

[英]servlet chain filter

How can I write a filter class to pass the response from one servlet to another along with the GET parameters? 如何编写过滤器类以将响应从一个servlet传递到另一个servlet以及GET参数?

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 这在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. 它不会转发到第二个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? 我没有调试环境设置,所以我不知道它在哪里失败,但是有人能指出我正确的方向吗?

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. FilterChain#doFilter()继续请求处理,仅在目标控制器完成其作业并且已呈现并提交响应时返回。

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() ). 如果你的目的是通过改变请求到不同的服务器端目的地你应该调用它RequestDispatcher#forward() (或当你想让客户端通过发送一个新的请求HttpServletResponse#sendRedirect() You should have noticed this by seeing IllegalStateException: response already committed in the server logs. 你应该注意到这一点,看到IllegalStateException: response already committed在服务器日志中IllegalStateException: response already committed

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. 具体问题无关 ,如果我理解/猜测您的实际功能需求是正确的,那么您需要在/process servlet中寻找RequestDispatcher#include() See also How do I execute multiple servlets in sequence? 另请参阅如何按顺序执行多个servlet?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 servlet中过滤器和链的用途是什么? - What is the use of filter and chain in servlet? request-filter-servlet-jsp链中的响应 - Response in the request-filter-servlet-jsp chain 带有 Mockito 的过滤器链 带有 Servlet 请求的单元测试 - Filter Chain with Mockito Unit Test with Servlet Request 修改过滤器链-或选择servlet以使用过滤器响应请求 - Modify the filter chain - Or select servlet to respond to request using filter Servlet筛选器重定向:如何在chain.doFilter(request,resp);之后进行重定向。 - Servlet Filter redirect: How to redirect after chain.doFilter(request,resp); Java Servlet 过滤器:我必须在传递到链之前添加标头,文档另有说明 - Java Servlet filter: I have to add headers before passing to the chain, documentation tells otherwise 在servlet请求上调用startAsync()会在Jetty 9的ProxyServlet终止的过滤器链中引发IllegalStateException - Calling startAsync() on a servlet request throws IllegalStateException in a filter chain terminated by Jetty 9's ProxyServlet URLrewrite过滤器中的过滤器链 - Filter chain in URLrewrite filter 测试类的自定义过滤器链? - Custom Filter Chain for TestClasses? 带过滤链的弹簧配置 - Spring configuration with filter chain
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM