title: Cannot forward after response has been committed Servlet error
body: <p>When I run this code, I get an IllegalStateException: Cannot forward after respons has been committed. I apologize, I am new to servlets. What should I do to fix this problem? This occurs at this line: </p>
  
  <pre><code>RequestDispatcher rd = request.getRequestDispatcher("/displayjobs.jsp");
  rd.forward(request, response);
  </code></pre>
  
  <p>Here is the stack trace of the exception:</p>
  
  <pre><code>java.lang.IllegalStateException: Cannot forward after response has been committed
  schedule.doGet(schedule.java:71)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
  </code></pre>
  
  <p>Here is the rest of my servlet code. 
  EDIT: I have also included my jsp code, as I am now experiencing a NPE at line 2:</p>
  
  <pre><code>1 List data = (List)request.getAttribute("jobsData);
  2 for(itr = data.iterator(); itr.hasNext();)
  </code></pre>
  
  <p>This means that for some reason data is null rather than having the information of JobsData.</p>
  
  <pre><code>/**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
      try{
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
          String connectionUrl = "jdbc:sqlserver://10.11.1.246;databaseName=Test;integratedSecurity=false;user=sa;password=S0l1dConcepts";
          Connection con = DriverManager.getConnection(connectionUrl);
          String query = "select * from jobs";
          stmt = con.createStatement();
          res = stmt.executeQuery(query);
          while(res.next()){
              list.add(res.getString(1));
              list.add(res.getString(2));
              list.add(res.getString(3));
              list.add(res.getString(4));
              list.add(res.getString(5));
              list.add(res.getString(6));
              list.add(res.getString(7));
              list.add(res.getString(8));
              list.add(res.getString(9));
              list.add(res.getString(10));
          }
          res.close();
      }
      catch(Exception e){
          RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
          rd.forward(request, response);
      }
  finally{
          request.setAttribute("jData", list);
          RequestDispatcher rd = request.getRequestDispatcher("/displayjobs.jsp");
          rd.forward(request, response);
          list.clear();
          out.close();
  }
  
  }
  
  
  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
      try{
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
          String connectionUrl = "jdbc:sqlserver://10.11.1.246;databaseName=Test;integratedSecurity=false;user=sa;password=S0l1dConcepts";
          Connection con = DriverManager.getConnection(connectionUrl);
          String query = "select * from jobs";
          stmt = con.createStatement();
          res = stmt.executeQuery(query);
          while(res.next()){
              list.add(res.getString(1));
              list.add(res.getString(2));
              list.add(res.getString(3));
              list.add(res.getString(4));
              list.add(res.getString(5));
              list.add(res.getString(6));
              list.add(res.getString(7));
              list.add(res.getString(8));
              list.add(res.getString(9));
              list.add(res.getString(10));
          }
          res.close();
      }
      catch(Exception e){
          RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
          rd.forward(request, response);
      }
      finally{
          request.setAttribute("jData", list);
          RequestDispatcher rd = request.getRequestDispatcher("/displayjobs.jsp");
          rd.forward(request, response);
          list.clear();
          out.close();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the JSP code:</p>
  
  <pre><code>&lt;%@page import="java.util.List" %&gt;
  &lt;%@page import="java.util.Iterator" %&gt;
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01   Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
   &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Scheduling&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;table&gt;
      &lt;%Iterator itr;%&gt;
      &lt;%List data = (List)request.getAttribute("jobsData");
      for(itr = data.iterator(); itr.hasNext();){
          %&gt;
      &lt;tr&gt;    
          &lt;% String s = (String) itr.next();%&gt;
          &lt;td&gt;&lt;%=s%&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%=itr.next()%&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%=itr.next()%&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%=itr.next()%&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%=itr.next()%&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%=itr.next()%&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%=itr.next()%&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%=itr.next()%&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%=itr.next()%&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%=itr.next()%&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input type="submit" value="Edit" name="edit" onclick ="editRecord(&lt;%=s%&gt; %&gt;);"&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input type="submit" value="Delete" name="delete" onclick ="deleteRecord(&lt;%=s%&gt; %&gt;);"&gt;&lt;/td&gt;
          &lt;%} %&gt;
          &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>
  </p>