title: java.lang.IllegalStateException: Cannot forward after response has been committed in servlet
body: <p>In my project, I prohibit a user every page only if he is already logged on. That is why I wrote the below code. When I type in browser, for example, <a href="http://localhost:8080/JSP1/Students" rel="nofollow noreferrer">http://localhost:8080/JSP1/Students</a>, I come to the <code>login.jsp</code> page. But after I input the loginid and password, only blank page <a href="http://localhost:8080/JSP1/Logged" rel="nofollow noreferrer">http://localhost:8080/JSP1/Logged</a> appears and GlassFish says there is an exception in </p>
  
  <pre><code>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
  </code></pre>
  
  <p>Complete code for doGet and doPost:</p>
  
  <pre><code> @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&lt;Student&gt; 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("&lt;p&gt;You have successfully edited a Student=" + studentToEdit.toString() + " to Student="+ newStudent.toString());
              } else {
                   out.println("&lt;p&gt;Unsuccessful attempt to edit!&lt;/p&gt;" );
              }
        }
  
  }
  </code></pre>
  
  <p>The login.jsp is simple:</p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;title&gt;JSP Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form action="/JSP1/Logged" method="POST"&gt;
      &lt;table&gt;           
      &lt;tr&gt;
          &lt;td&gt;Login:&lt;/td&gt;
          &lt;td&gt;&lt;input type="text" name="login" value=""/&gt;  &lt;/td&gt;   
      &lt;/tr&gt;
      &lt;tr&gt;
           &lt;td&gt;Password &lt;/td&gt;
           &lt;td&gt;&lt;input type="password" name="password"/&gt; &gt;&lt;/td&gt;
  
      &lt;/tr&gt;
       &lt;tr&gt;
           &lt;td&gt;&lt;input type="submit" name="OK" value="OK" /&gt; &lt;/td&gt;
      &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>I can't figure out what's happening.</p>