servlets – java.lang.IllegalStateException:在响应提交后无法转发/ sendRedirect

这个方法抛出

java.lang.IllegalStateException: Cannot forward after response has been committed

我无法发现问题。任何帮助?

    int noOfRows = Integer.parseInt(request.getParameter("noOfRows"));
    String chkboxVal = "";
    // String FormatId=null;
    Vector vRow = new Vector();
    Vector vRow1 = new Vector();
    String GroupId = "";
    String GroupDesc = "";
    for (int i = 0; i < noOfRows; i++) {
        if ((request.getParameter("chk_select" + i)) == null) {
            chkboxVal = "notticked";
        } else {
            chkboxVal = request.getParameter("chk_select" + i);
            if (chkboxVal.equals("ticked")) {
                fwdurl = "true";
                Statement st1 = con.createStatement();
                GroupId = request.getParameter("GroupId" + i);
                GroupDesc = request.getParameter("GroupDesc" + i);
                ResultSet rs1 = st1
                        .executeQuery("select FileId,Description from cs2k_Files "
                                + " where FileId like 'M%' and co_code = "
                                + ccode);
                ResultSetMetaData rsm = rs1.getMetaData();
                int cCount = rsm.getColumnCount();

                while (rs1.next()) {
                    Vector vCol1 = new Vector();
                    for (int j = 1; j <= cCount; j++) {
                        vCol1.addElement(rs1.getObject(j));
                    }
                    vRow.addElement(vCol1);
                }
                rs1 = st1
                        .executeQuery("select FileId,NotAllowed from cs2kGroupSub "
                                + " where FileId like 'M%' and GroupId = '"
                                + GroupId + "'" + " and co_code = " + ccode);
                rsm = rs1.getMetaData();
                cCount = rsm.getColumnCount();

                while (rs1.next()) {
                    Vector vCol2 = new Vector();
                    for (int j = 1; j <= cCount; j++) {
                        vCol2.addElement(rs1.getObject(j));
                    }
                    vRow1.addElement(vCol2);
                }

                // throw new Exception("test");

                break;
            }
        }
    }
    if (fwdurl.equals("true")) {
        // throw new Exception("test");
        // response.sendRedirect("cs2k_GroupCopiedUpdt.jsp") ;
        request.setAttribute("GroupId", GroupId);
        request.setAttribute("GroupDesc", GroupDesc);
        request.setAttribute("vRow", vRow);
        request.setAttribute("vRow1", vRow1);
        getServletConfig().getServletContext().getRequestDispatcher(
                "/GroupCopiedUpdt.jsp").forward(request, response);
    }
启动器之间的一个常见的误解是,他们认为forward(),sendRedirect()或sendError()的调用将神奇地退出并“跳出”方法块,从而忽略代码的残余。例如:

protected void doPost() {
    if (someCondition) {
        sendRedirect();
    }
    forward(); // This is STILL invoked when someCondition is true!
}

因此,这实际上不是真的。它们的行为当然不同于任何其他Java方法(期望System#exit())。当上面的例子中的someCondition为true并且因此在相同的请求/响应上调用sendRedirect()或sendError()之后调用forward()时,那么机会很大,你会得到异常:

java.lang.IllegalStateException: Cannot forward after response has been committed

如果if语句调用forward(),然后调用sendRedirect()或sendError(),则会抛出以下异常:

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

要解决这个问题,你需要添加一个返回;语句后

protected void doPost() {
    if (someCondition) {
        sendRedirect();
        return;
    }
    forward();
}

…或引入else块。

protected void doPost() {
    if (someCondition) {
        sendRedirect();
    } else {
        forward();
    }
}

要找出代码中的根本原因,只需搜索调用forward(),sendRedirect()或sendError()的任何行,而不退出方法块或跳过代码的剩余部分。这可以在特定代码行之前的同一个servlet内部,也可以在特定servlet之前被调用的任何servlet或过滤器中。

在sendError()的情况下,如果您的唯一目的是设置响应状态,请改用setStatus()。

另一个可能的原因是,servlet写入响应,而forward()将被调用,或者在同一个方法中被调用。

protected void doPost() {
    out.write("some string");
    // ... 
    forward(); // Fail!
}

响应缓冲区大小默认在大多数服务器为2KB,所以如果你写超过2KB到它,那么它将被提交和forward()将失败以同样的方式:

java.lang.IllegalStateException: Cannot forward after response has been committed

解决方案很明显,只是不要写入servlet中的响应。这是JSP的责任。你只需设置一个请求属性,像request.setAttribute(“data”,“some string”),然后在JSP中打印,像这样$ {data}。另请参见our Servlets wiki page以了解如何以正确的方式使用Servlet。

与您的具体问题无关,您的JDBC代码泄漏资源。修正这一点。有关提示,另见How often should Connection, Statement and ResultSet be closed in JDBC?

http://stackoverflow.com/questions/2123514/java-lang-illegalstateexception-cannot-forward-sendredirect-after-response-ha

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:servlets – java.lang.IllegalStateException:在响应提交后无法转发/ sendRedirect