java.lang.IllegalStateException:在servlet中提交响应后无法转发

前端之家收集整理的这篇文章主要介绍了java.lang.IllegalStateException:在servlet中提交响应后无法转发前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed8个
在我的项目中,我禁止用户每个页面只有他已经登录.这就是我写下面代码的原因.当我输入浏览器时,例如 http://localhost:8080/JSP1/Students,我来到login.jsp页面.但在输入loginid和密码后,只显示空白页面 http://localhost:8080/JSP1/Logged,而GlassFish表示存在异常
  1. if (userPath.equals("/Students")){
  2. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
  3. requestDispatcher.forward(request,response);
  4. }
  5.  
  6. java.lang.IllegalStateException: PWC1227: Cannot forward after response has been committed

完整的doGet和doPost代码

  1. @Override
  2. protected void doGet(HttpServletRequest request,HttpServletResponse response)
  3. throws ServletException,IOException {
  4.  
  5. HttpSession ses = request.getSession();
  6. String login = (String)ses.getAttribute("login");
  7. String password = (String)ses.getAttribute("password");
  8. if ((login==null)|(password==null)){
  9. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
  10. requestDispatcher.forward(request,response);
  11. }
  12.  
  13. //Now we think that we are successfully logged in
  14.  
  15. String userPath = request.getServletPath();
  16. // System.out.println(userPath);
  17.  
  18. if (userPath.equals("/Login")){
  19. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
  20. requestDispatcher.forward(request,response);
  21. }
  22.  
  23. if (userPath.equals("/Students")){
  24. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
  25. requestDispatcher.forward(request,response);
  26. }
  27.  
  28. if (userPath.equals("/Student")){
  29. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Student.jspx");
  30. requestDispatcher.forward(request,response);
  31. }
  32.  
  33. if (userPath.equals("/StudentEdit")){
  34. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/StudentEdit.jsp");
  35. requestDispatcher.forward(request,response);
  36. }
  37.  
  38. }
  39.  
  40.  
  41. @Override
  42. protected void doPost(HttpServletRequest request,IOException {
  43. // processRequest(request,response);
  44. PrintWriter out = response.getWriter();
  45.  
  46. String userPath = request.getServletPath();
  47. System.out.println(userPath);
  48.  
  49. if (request.getRequestURI().equals("/Logged")){
  50. String Login = request.getParameter("login");
  51. String Password = request.getParameter("password");
  52. request.getSession().setAttribute("login",Login);
  53. request.getSession().setAttribute("password",Password);
  54. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
  55. requestDispatcher.forward(request,response);
  56.  
  57. }
  58.  
  59. if (userPath.equals("/addStudent")) {
  60.  
  61. // System.out.println(request.getContextPath());
  62. String Name = request.getParameter("name");
  63. String Surname = request.getParameter("surname");
  64. String Login = request.getParameter("login");
  65. String Password = request.getParameter("password");
  66. Student student = new Student(Name,Surname,Login,Password);
  67. if (student != null) {
  68. dao.insertStudent(student);
  69. } else {
  70. System.out.println("Not valid parameter!!!");
  71. }
  72. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
  73. requestDispatcher.forward(request,response);
  74. }
  75.  
  76. if (request.getRequestURI().equals("/Edit")) {
  77. System.out.println("We work with students!!!");
  78. String delete = request.getParameter("Add_new_student");
  79. if (delete != null){
  80. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Student.jspx");
  81. requestDispatcher.forward(request,response);
  82. }
  83.  
  84. Enumeration parameters = request.getParameterNames();
  85. while (parameters.hasMoreElements()) {
  86. String parameterName = (String) parameters.nextElement();
  87. String parameterValue = request.getParameter(parameterName);
  88. String norder = parameterName.substring(parameterName.indexOf("_")+1);
  89.  
  90. ArrayList<Student> curStudents = dao.getAllStudents();
  91. int norderint = Integer.parseInt(norder);
  92. Student studentToWork = curStudents.get(norderint);
  93.  
  94. String actionToDo = parameterName.substring(0,parameterName.indexOf("_"));
  95.  
  96. if (actionToDo.equals("Edit")){
  97.  
  98. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/StudentEdit.jsp");
  99. ServletContext cont = request.getServletContext();
  100. cont.setAttribute("studentToEdit",studentToWork);
  101. requestDispatcher.forward(request,response);
  102. } else {
  103. boolean attemp = dao.deleteStudent(studentToWork);
  104. if (attemp){
  105. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
  106. requestDispatcher.forward(request,response);
  107. } else {
  108. out.println("Unsuccessfull attemp to delete a Student");
  109. }
  110. }
  111. }
  112. }
  113.  
  114. if (userPath.equals("/EditStudent")){
  115. System.out.println("We work with StudentEdit!");
  116. Student studentToEdit = (Student)request.getSession().getAttribute("studentToEdit");
  117. String newName = request.getParameter("name");
  118. String newSurname = request.getParameter("surname");
  119. String newLogin = request.getParameter("login");
  120. String newPassword = request.getParameter("password");
  121. Student newStudent = new Student(newName,newSurname,newLogin,newPassword);
  122. boolean update = dao.updateStudent(studentToEdit,newStudent);
  123. if (update){
  124. out.println("<p>You have successfully edited a Student=" + studentToEdit.toString() + " to Student="+ newStudent.toString());
  125. } else {
  126. out.println("<p>Unsuccessful attempt to edit!</p>" );
  127. }
  128. }
  129.  
  130. }

login.jsp很简单:

  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>JSP Page</title>
  7. </head>
  8. <body>
  9. <form action="/JSP1/Logged" method="POST">
  10. <table>
  11. <tr>
  12. <td>Login:</td>
  13. <td><input type="text" name="login" value=""/> </td>
  14. </tr>
  15. <tr>
  16. <td>Password </td>
  17. <td><input type="password" name="password"/> ></td>
  18.  
  19. </tr>
  20. <tr>
  21. <td><input type="submit" name="OK" value="OK" /> </td>
  22. </tr>
  23. </table>
  24. </form>
  25. </body>

我无法弄清楚发生了什么.

解决方法

如果未提供登录和/或密码,您不会在转发后返回.在初学者中常见的误解是,forward()方法神奇地终止了代码执行并以某种方式跳出了方法.因此不是这样.您必须从方法返回并自行停止执行代码的残余.

你需要添加一个回报;

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

或者添加其他内容

  1. if ((login==null)|(password==null)){
  2. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
  3. requestDispatcher.forward(request,response);
  4. } else {
  5. // Now we think that we are successfully logged in.
  6.  
  7. // Yes,that above comment is now finally true.
  8. // Put your bunch of non-DRY if-else code here.
  9. }

也可以看看:

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

猜你在找的Java相关文章