title: how to fix Cannot call sendRedirect() after the response has been committed?
body: <p>I am trying to pass values from servlet to jsp page using the code below:  </p>
  
  <pre><code>public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
      PrintWriter out = response.getWriter();
      try {
          System.out.println("try");
  
          String taskid=request.getParameter("id");   
          convty = new Connectivity();
          con = convty.setConnection();
          st=con.createStatement();
  
          query="select * from task_table";
  
          rset=convty.getResultSet(query, con);
          while(rset.next()) {
              System.out.println("while");
              lst.add(rset.getString("task_id"));
              lst.add(rset.getString("date"));
              lst.add(rset.getString("project_name"));    
          }
          rset.close();
          System.out.println("after while");
  
      } catch(Exception e) {
  
          RequestDispatcher dd= request.getRequestDispatcher("error.jsp");
          dd.forward(request, response);
  
      } finally {
          System.out.println("finally1");
          HttpSession sessionvar = request.getSession();
          sessionvar.setAttribute("TaskData", "lst");
  
          response.sendRedirect("usertaskpage.jsp");
  
          lst.clear();
          out.close();            
      }
  }
  </code></pre>
  
  <p>when I run my page I am getting:</p>
  
  <p><strong>Error :</strong></p>
  
  <pre><code>java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
      org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:483)
      src.Taskservlet.doPost(Taskservlet.java:108)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
  </code></pre>
  
  <p>I tried to use: </p>
  
  <pre><code>RequestDispatcher dd=request.getRequestDispatcher("usertaskpage.jsp");
  dd.forward(request, response);
  </code></pre>
  
  <p>but I got the same error.</p>
  
  <p>How to fix this error?</p>