0

可能重复:
java.lang.IllegalStateException:提交响应后无法转发

这种错误的通常原因是什么:

com.mycompany.myapp.servlet.TxnDetailsServlet doRequest
ERROR: View failed
java.lang.IllegalStateException: Cannot forward after response has been committed
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:312)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
 at com.mycompany.myapp.servlet.TxnDetailsServlet.doRequest(TxnDetailsServlet.java:82)
 at com.mycompany.myapp.servlet.TxnDetailsServlet.doGet(TxnDetailsServlet.java:131)

servlet 处理请求(即设置属性)然后调用:

    private void doRequest(HttpServletRequest request) throws IOException, ServletException     {
        // Code omitted
        getServletContext().getRequestDispatcher("/Some.jsp").forward(this.request, this.response);
        // Code omitted
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doGet(request, response);
        doRequest(request);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request, response);
        doRequest(request);
    }

servlet 不做任何响应。

4

2 回答 2

0

如果您转发到另一个 servlet/jsp,请不要写入响应输出流。

于 2012-12-12T08:08:31.323 回答
0

添加到其他人所说的,响应不需要一次转移到客户端(浏览器)。相反,每当您调用 response.flushBuffer 时,它都可以在多个镜头中传输到客户端。一旦响应开始向客户端传输数据,您就不能做任何改变响应状态的事情(setStatus、forward 等)

于 2012-12-12T08:21:42.057 回答