title: Getting an IllegalStateException when i call forward()
body: <p>I am trying to learn abt servlets.
  I am getting a IllegalStateException when i call forward(). The error occurs in the LoginBean.do if block at the point where i call forward.</p>
  
  <pre><code>        process(request,response);
  }
  
  private void process(HttpServletRequest request, HttpServletResponse response) {
      // TODO Auto-generated method stub
      String uri = request.getRequestURI();
      RequestDispatcher rd = null;
      CAppModel model = new CAppModel();
  
      System.out.println("Inside process ()");
      try{
      if(uri.contains("/Login"))
      {
          System.out.println("Inside Login.do");
          rd = request.getRequestDispatcher("Login.jsp");
          rd.forward(request,response);
      }
  
      if(uri.contains("/Register.do"))
      {
          System.out.println("inside Register.do");
      }
  
      if(uri.contains("/LoginBean.do"))
      {
  
          System.out.println("Inside LoginBean.do");
          LoginBean lb = (LoginBean)request.getAttribute("login");
          System.out.println("toString(): "+lb.toString());
          String result = model.authenticate(lb);
          System.out.println("CS: result of auth "+result);
  
          if(result.equals("Success"))
          {
              System.out.println("Inside if Success of loginbean");
              HttpSession session = request.getSession(true);
              session.setAttribute("user", lb.getEmail());
              rd = request.getRequestDispatcher("Menu.jsp");
              rd.forward(request, response);
  
          }
          else
          {
              System.out.println("failed auth "+result);
              request.setAttribute("errorMsg", result);
              rd = request.getRequestDispatcher("Login.jsp");             
              rd.forward(request,response);
          }
  
  
      }
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
  }
  
  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      process(request,response);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I am getting the following error</p>
  
  <pre><code>java.lang.IllegalStateException: Cannot forward after response has been committed
  at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:328)
  at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
  at com.aditya.CApp.ControllerServlet.process(ControllerServlet.java:69)
  at com.aditya.CApp.ControllerServlet.doGet(ControllerServlet.java:31)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721)
  at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466)
  at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
  at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
  at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:741)
  at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:711)
  at org.apache.jsp.LoginInt_jsp._jspService(LoginInt_jsp.java:104)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1527)
  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1484)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  at java.lang.Thread.run(Unknown Source)
  </code></pre>