RequestDispatcher and flushBuffer

Discussions

Web tier: servlets, JSP, Web frameworks: RequestDispatcher and flushBuffer

  1. RequestDispatcher and flushBuffer (1 messages)

    HI,
    According to Sun specifications for servlets,

    "The forward method of the RequestDispatcher interface may be called by the
    calling servlet only when no output has been committed to the client. If output data
    exists in the response buffer that has not been committed, the content must be
    cleared before the target servlet’s service method is called. If the response has been
    committed, an IllegalStateException must be thrown."

    --------------------------------------------------
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;


    public class requestWriter extends HttpServlet{

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException,ServletException
    {
    PrintWriter out = response.getWriter();

    out.println("I am the source servlet ");

    response.flushBuffer();

    RequestDispatcher rd = request.getRequestDispatcher("requestWriter1");

    rd.forward(request,response);

    out.println("<BR> is committed <BR>"+response.isCommitted());


    }
    }

    --------------------------------------------------

    This servlet was deployed to Tomcat 4.1.6 . The output was
    "I am the source servlet"
    According to Sun specifications it should have thrown the IllegalStateException
    bcaz i called RequestDispatcher.forward method after committing the response.

    Need your comments.
  2. RequestDispatcher and flushBuffer[ Go to top ]

    How do you know that the servlet has not thrown the exception?

    try this...
    --------------------------------------------------
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;


    public class requestWriter extends HttpServlet{

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException,ServletException
    {
    PrintWriter out = response.getWriter();

    out.println("I am the source servlet ");

    response.flushBuffer();

    try { // -- added
    RequestDispatcher rd = request.getRequestDispatcher("requestWriter1");

    rd.forward(request,response);

    out.println("<BR> is committed <BR>"+response.isCommitted());
    } catch(Exception ex){ // -- added
      System.out.println("exception thrown") ; // -- added
      ex.printStackTrace() ; // -- added
    } // -- added

    }
    }

    --------------------------------------------------