[英]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.