title: RequestDispatcher - when is a response committed?
body: <p>I'm writing some basic code as I learn about the RequestDispatcher. I understand that when rd.forward() is called, control (and response handling) is forwarded to the resource named in the path - in this case another servlet. But why doesn't this code throw an IllegalStateException (not that I want one) due to the out.write() statements earlier in the code?</p>
  
  <p>I guess what I'm really asking is, how or when would these out.write() statements be committed?</p>
  
  <p>Thanks,
  Jeff</p>
  
  <pre><code>public class Dispatcher extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws IOException, ServletException{
  
      response.setContentType("text/plain");
      PrintWriter out = response.getWriter();
      out.write("In Dispatcher\n");
  
      String modeParam = request.getParameter("mode");
      String path = "/Receiver/pathInfo?fruit=orange";
      RequestDispatcher rd = request.getRequestDispatcher(path);
      if(rd == null){
          out.write("Request Dispatcher is null. Please check path.");
      }
          if(modeParam.equals("forward")){
              out.write("forwarding...\n");
              rd.forward(request, response);
          }
          else if(modeParam.equals("include")){
              out.write("including...\n");
              rd.include(request, response);
          }
      out.flush();
      out.close();
  }
  </code></pre>
  
  <p>}</p>