title: Cannot forward after response has been committed
body: <p>I am trying to forward from Servlet to JSP along setting attribute but I am getting the following exception: </p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Cannot forward after response has
    been committed</p>
  </blockquote>
  
  <p>Please help.</p>
  
  <p>My code:</p>
  
  <pre><code>public class setPassword extends HttpServlet {
  
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException, ClassNotFoundException, SQLException {
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
  
      Connection co;
      try {
          Class.forName("com.mysql.jdbc.Driver");
          co = DriverManager.getConnection("jdbc:mysql://localhost:3306/gift", "root", "");
          Statement st = co.createStatement();
          System.out.println("setpassword...........");
  
          ResultSet rs = st.executeQuery("select * from generaldata");
  
          System.out.println("setpassword...........111");
          String userid = request.getParameter("username");
  
          while (rs.next()) {
              String us = rs.getString(2);
              String ps = rs.getString(3);
              System.out.println("setpassword...........222");
  
              System.out.println(us);
              System.out.println(userid);
  
              if (userid.equals(us)) {
  
                  System.out.println(ps);
                  System.out.println("setpassword...........333");
  
                  if (ps.equals("")) {
                      String pass = request.getParameter("password");
                      String pass1 = request.getParameter("password1");
                      System.out.println("setpassword...........444");
  
                      if (pass.equals(pass1)) {
                          rs.updateNString(3, pass);
                          RequestDispatcher rdpassword = request.getRequestDispatcher("home.jsp");
                          rdpassword.forward(request, response);
                          System.out.println("setpassword...........555");
                      } else {
                          request.setAttribute("errorPassword", "Your Password does not match...!!!");
                          RequestDispatcher rdlogin = request.getRequestDispatcher("newuser.jsp");
                          rdlogin.forward(request, response);
                          System.out.println("setpassword...........666");
                      }
                  } else {
                      request.setAttribute("err", "Please note you are not a New User...!!!");
                      System.out.println("setpassword.................777");
                      RequestDispatcher rdNotNew = request.getRequestDispatcher("login.jsp");
                      rdNotNew.forward(request, response);
  
                  }
              } else {
                  request.setAttribute("err", "Not a valid User...!!!");
                  System.out.println("setpassword.................788");
                  RequestDispatcher rdNotValid = request.getRequestDispatcher("login.jsp");
                  rdNotValid.forward(request, response);
              }
              System.out.println("setpassword.................888");
          }
          System.out.println("setpassword.................999");
  
          rs.close();
          st.close();
          co.close();
  
      } catch (Exception e) {
          System.out.println(e);
      } finally {
          out.close();
      }
  }
  </code></pre>