servlet 链过滤器
如何编写一个过滤器类以将响应连同 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FilterChain#doFilter() 继续请求处理,并且仅在目标控制器完成其工作并且呈现并提交响应时才返回。
如果您的目的是通过
RequestDispatcher#forward()
将请求更改为不同的服务器端目标(或者当您想让客户端发送由HttpServletResponse#sendRedirect()
发出的新请求)。您应该通过在服务器日志中查看IllegalStateException:响应已提交
注意到这一点。因此,要么删除它,以便您只得到转发,
要么将其设置
为与具体问题无关的条件,如果我理解/猜测您的实际功能需求是正确的,那么您会更寻找
/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 byHttpServletResponse#sendRedirect()
). You should have noticed this by seeingIllegalStateException: response already committed
in the server logs.So, either remove it so that you only end up with the forward,
or make it a conditional
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?