java. Lang.illegalstateexception: the response submitted in the servlet cannot be forwarded

See English answer > java Lang. IllegalStateException: cannot (forward | sendredirect | create session) after response has been committed8

if (userPath.equals("/Students")){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
        requestDispatcher.forward(request,response);
    }

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

Complete doget and dopost Codes:

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

     HttpSession ses = request.getSession();
     String login = (String)ses.getAttribute("login");
     String password = (String)ses.getAttribute("password");
     if ((login==null)|(password==null)){
         RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
         requestDispatcher.forward(request,response);
     } 

     //Now we think that we are successfully logged in

    String userPath = request.getServletPath();
   // System.out.println(userPath);

    if (userPath.equals("/Login")){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
        requestDispatcher.forward(request,response);
    }

    if (userPath.equals("/Students")){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
        requestDispatcher.forward(request,response);
    }

     if (userPath.equals("/Student")){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Student.jspx");
        requestDispatcher.forward(request,response);
    }

     if (userPath.equals("/StudentEdit")){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/StudentEdit.jsp");
        requestDispatcher.forward(request,response);
    }

}


@Override
protected void doPost(HttpServletRequest request,IOException {
  //  processRequest(request,response);
    PrintWriter out = response.getWriter();

    String userPath = request.getServletPath();
    System.out.println(userPath);

     if (request.getRequestURI().equals("/Logged")){
            String Login = request.getParameter("login");
            String Password = request.getParameter("password");
            request.getSession().setAttribute("login",Login);
            request.getSession().setAttribute("password",Password);
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
            requestDispatcher.forward(request,response);

        }

    if (userPath.equals("/addStudent")) {

       //     System.out.println(request.getContextPath());
            String Name = request.getParameter("name");
            String Surname = request.getParameter("surname");
            String Login = request.getParameter("login");
            String Password = request.getParameter("password");
            Student student = new Student(Name,Surname,Login,Password);
            if (student != null) {
                dao.insertStudent(student);
            } else {
                System.out.println("Not valid parameter!!!");
            }
             RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
             requestDispatcher.forward(request,response);
        }

    if (request.getRequestURI().equals("/Edit")) {
            System.out.println("We work with students!!!");
            String delete = request.getParameter("Add_new_student");
            if (delete != null){
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Student.jspx");
                requestDispatcher.forward(request,response);
            }

            Enumeration parameters = request.getParameterNames();
            while (parameters.hasMoreElements()) {
                String parameterName = (String) parameters.nextElement();
                String parameterValue = request.getParameter(parameterName);
                String norder = parameterName.substring(parameterName.indexOf("_")+1);

                ArrayList<Student> curStudents = dao.getAllStudents();
                int norderint = Integer.parseInt(norder);
                Student studentToWork = curStudents.get(norderint);

                String actionToDo = parameterName.substring(0,parameterName.indexOf("_"));

                if (actionToDo.equals("Edit")){

                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/StudentEdit.jsp");
                ServletContext cont = request.getServletContext();
                cont.setAttribute("studentToEdit",studentToWork);
                requestDispatcher.forward(request,response);
                } else {
                    boolean attemp = dao.deleteStudent(studentToWork);
                    if (attemp){
                         RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
                         requestDispatcher.forward(request,response);
                    } else {
                        out.println("Unsuccessfull attemp to delete a Student");
                    }
                }
            }
        }

      if (userPath.equals("/EditStudent")){
            System.out.println("We work with StudentEdit!");
            Student studentToEdit = (Student)request.getSession().getAttribute("studentToEdit");
            String newName = request.getParameter("name");
            String newSurname = request.getParameter("surname");
            String newLogin = request.getParameter("login");
            String newPassword = request.getParameter("password");
            Student newStudent = new Student(newName,newSurname,newLogin,newPassword);
            boolean update = dao.updateStudent(studentToEdit,newStudent);
            if (update){
            out.println("<p>You have successfully edited a Student=" + studentToEdit.toString() + " to Student="+ newStudent.toString());
            } else {
                 out.println("<p>Unsuccessful attempt to edit!</p>" );
            }
      }

}

login. JSP is simple:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <form action="/JSP1/Logged" method="POST">
    <table>           
    <tr>
        <td>Login:</td>
        <td><input type="text" name="login" value=""/>  </td>   
    </tr>
    <tr>
         <td>Password </td>
         <td><input type="password" name="password"/> ></td>

    </tr>
     <tr>
         <td><input type="submit" name="OK" value="OK" /> </td>
    </tr>
    </table>
</form>
</body>

I can't figure out what happened

Solution

If you do not provide a login and / or password, you will not return after forwarding A common misconception among beginners is that the forward () method magically terminates code execution and somehow jumps out of the method So it's not You must return from the method and stop executing the remainder of the code yourself

You need to add a return;

if ((login==null)|(password==null)){
    RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
    requestDispatcher.forward(request,response);
    return;
}

Or add something else

if ((login==null)|(password==null)){
    RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
    requestDispatcher.forward(request,response);
} else {
    // Now we think that we are successfully logged in.

    // Yes,that above comment is Now finally true.
    // Put your bunch of non-DRY if-else code here.
}

You can also see:

> java. lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>