一尘不染

java.lang.IllegalStateException:在Servlet中提交响应后无法转发[重复]

jsp

在我的项目中,仅当用户已经登录时,我才禁止用户访问每个页面。这就是为什么我编写以下代码。当我输入浏览器时,例如http:// localhost:8080/ JSP1 /Students,我进入login.jsp页面。但是在我输入了登录ID和密码后,仅出现空白页http://localhost:8080 / JSP1 /Logged,GlassFish表示在

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

doGet和doPost的完整代码:

 @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, HttpServletResponse response)
        throws ServletException, 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很简单:

<%@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>

我不知道发生了什么事。


阅读 649

收藏
2020-06-08

共1个答案

一尘不染

未提供登录名和/或密码时,您不会在前进后返回。入门者之间普遍存在误解,即该forward()方法神奇地终止了代码执行并以某种方式跳出了该方法。因此,这是不正确的。您必须从方法中返回并自己停止执行代码的剩余部分。

您需要添加一个 return;

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

或添加一个 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.
}
2020-06-08