IllegalStateException的原因:在提交响应后无法转发

时间:2012-12-12 08:04:47

标签: java jsp servlets

  

可能重复:
  java.lang.IllegalStateException: Cannot forward after response has been committed

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

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在响应中没有做任何事情。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

添加到其他人所说的内容,无需一次性将响应转移到客户端(浏览器)。相反,只要你调用response.flushBuffer,它就可以在多个镜头中转移到客户端。一旦响应开始将数据传输到客户端,您就无法做任何改变响应状态的事情(setStatus,forward etc)