Hello all,

I have some confusion about RequestDispatcher.forward vs
RequestDispatch.include.  The spec and javadoc say some helpful things but
leave other questions open.

For example, what happens to the code after a forward?  Is it executed?  It
turns out that it is, which leads to more questions:
 - Can you still write to the output stream?
 - If so, what really is the difference between a forward and an include?

I've done some playing with this under WebLogic 5.1.  But since it's not
really detailed in the spec, could it be that other servlet engines do
things differently?

If anybody has experiences, comments, or corrections, i'm interested in
hearing them.

Anyway, here are some of my tests:


The following servlet will throw an exception to the servlet engine, so code
after a forward IS executed.

public class ForwardServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
                RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/test/AnotherPage.jsp");
                dispatcher.forward(req, resp);
                throw new ServletException("Test exception");
    }
}

In fact, the following code will print "test" to the output stream, so you
CAN keep on writing.

public class ForwardServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
                RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/test/AnotherPage.jsp");
                dispatcher.forward(req, resp);
                resp.getOutputStream().print("test");
    }
}

As far as i can tell so far, include and forward are the same thing, except
that:
1. an included page may not touch the response code or HTTP headers.
2. when you forward, any unflushed buffers are cleared
3. you cannot forward after flushing buffers

I haven't tested yet, but i wonder if you can forward twice from the same
servlet doGet().

Example of 2, the following code will display the forwarded page but will
not print "test":

public class ForwardServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
                resp.getOutputStream().print("test");
                RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/test/IncludedPage.jsp");
                dispatcher.forward(req, resp);
    }
}

Of course the following code will print "test" as well as the included page:

public class ForwardServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
                resp.getOutputStream().print("test");
                RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/test/IncludedPage.jsp");
                dispatcher.include(req, resp);
    }
}

Example of 3, the following forward does not work:

public class ForwardServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
                resp.getOutputStream().print("test");
                resp.getOutputStream().flush();
                RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/test/IncludedPage.jsp");
                dispatcher.forward(req, resp);
    }
}

But the following include does:

public class ForwardServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
                resp.getOutputStream().print("test");
                resp.getOutputStream().flush();
                RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/test/IncludedPage.jsp");
                dispatcher.include(req, resp);
    }
}

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to