非法状态异常:在提交响应后无法调用sendRedirect()

时间:2016-09-06 18:18:30

标签: java jsp servlets response illegalstateexception

我调用GET方法来检索数据并使用RequestDispatcher在JSP页面上显示。后来我调用POST来保存它,同时使用sendRedirect停留在同一页面上。我得到非法的州例外。我是新的。请帮忙。

这是我的logout.jsp页面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Success Page</title>
</head>
<body>
    <form action="LogoutUserServlet" method="post">
        <input type="submit" value="Logout" name="Logout">
    </form>

    <form action="LogoutUserServlet" method="get">
        <input type="submit" value="Get Tracker">
    </form>

    <form action="LogoutUserServlet" method="post">

    <table cellpadding="5" border="1">

        <c:set var="i" value="0" />
        <c:set var="eo" value="1" />


        <c:forEach items="${sites}" var="s1" begin="${i}">

            <c:if test="${eo==1 || eo%2 ==1 }">
                    <tr>
                        <c:forEach items="${sites}" var="s1" begin="${i}" end="${i+2}">
                            <td class="a-center "><input type="checkbox" class="flat"
                                name="table_records" value="${s1}"> <c:out value="${s1}" /></td>
                        </c:forEach>
                    </tr>
                    <c:set var="i" value="${i+3}" />
                </c:if>


                <c:if test="${eo%2 ==0}">
                    <tr>
                        <c:forEach items="${sites}" var="s1" begin="${i}" end="${i+2}">
                            <td class="a-center "><input type="checkbox" class="flat"
                                name="table_records" value="${s1}"> <c:out value="${s1}" /></td>
                        </c:forEach>
                    </tr>
                    <c:set var="i" value="${i+3}" />
                </c:if>


                <c:set var="eo" value="${eo+2}" />
            </c:forEach>
        </table>
        <input type="submit" name="Save" value="save">
    </form>



</body>
</html>

这是我的LogoutUserServlet servlet页面

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    ArrayList<String> sitetypes = new ArrayList<String>();
    sitetypes.add("abc");  
    sitetypes.add("def");  
    sitetypes.add("ghi");  
    request.setAttribute("sites", sitetypes);
    request.getRequestDispatcher("/logout.jsp").forward(request, response);

}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if(request.getParameter("Logout")!=null){
        response.setContentType("text/html");
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("JSESSIONID")){
                    System.out.println("JSESSIONID="+cookie.getValue());
                    break;
                }
            }
        }
        //invalidate the session if exists
        HttpSession session = request.getSession(false);
        System.out.println("User="+session.getAttribute("user"));
        if(session != null){
            session.invalidate();
        }
        response.sendRedirect("login.html");

    } else if(request.getParameter("Save")!=null){
        String[] selectedStudentIds = request.getParameterValues("table_records");
        for (int i = 0; i < selectedStudentIds.length; i++) {
            System.out.println("=="+selectedStudentIds[i]); 
            response.sendRedirect("logout.jsp");

        }
    }
}

这是错误

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:482)
servlets.LogoutUserServlet.doPost(LogoutUserServlet.java:79)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
servlets.AuthenticationFilter.doFilter(AuthenticationFilter.java:45)

0 个答案:

没有答案