[ Team LiB ] Previous Section Next Section

Forwarding to Another Page

In addition to including another page, you can transfer to another page without returning to the original. This technique is called forwarding. When you forward the request on to another page (or servlet), the forwarding page is no longer involved in handling the request. You typically would use forwarding to handle an error, or if you need several different response pages that depend on the data in the request. In the latter case, a JSP or a servlet looks at the incoming request, decides which response page to use, and forwards the request on to the proper response page.

Forwarding to Another Page from a JSP

The JSP syntax for forwarding is very similar to the syntax for including. You use the <jsp:forward> tag like this:


<jsp:forward page="destinationPage"/>

When you forward to another page or servlet, your original page is replaced with the new page. That is, any output you might have sent is cleared.

Forwarding and Buffering

graphics/watchout_icon.gif

If you get an IllegalStateException when forwarding to another page, you might not be buffering the page. Make sure you turn the buffering on using the page directive.


Listing 10.8 shows a very simple JSP that forwards itself to another page.

Listing 10.8 Source Code for MainForwarder.jsp
<html>
<body>

You should never see me because my output is erased before forwarding.

<jsp:forward page="ForwardedPage.jsp"/>
</body>
</html>

As you can see in Figure 10.4, the text in the MainForwarder.jsp file doesn't show in the browser because it is erased before the ForwardedPage.jsp page executes.

Figure 10.4. When a JSP forwards to another JSP, the output from the original JSP is lost.

graphics/10fig04.gif

Forwarding to Another Page from a Servlet

Just as the syntax for forwarding from a JSP is similar to the syntax for including, the syntax for forwarding from a servlet is also very similar to the syntax for including. You again use the RequestDispatcher object, only this time, instead of calling its include method, you call its forward method:


RequestDispatcher d =
        request.getRequestDispatcher("destinationURL");
d.forward(request, response);

Listing 10.9 shows an example servlet that forwards to the same ForwardedPage.jsp page used by the JSP in Listing 10.8. Again, the output from the servlet is erased before the forwarded page runs.

Listing 10.9 Source Code for ForwarderServlet.java
package examples;

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

public class ForwarderServlet extends GenericServlet
{
    public void service(ServletRequest request,
        ServletResponse response)
        throws IOException, ServletException
    {
// Tell the web server that the response is HTML.
        response.setContentType("text/html");

// Get the PrintWriter for writing out the response.
        PrintWriter out = response.getWriter();

// Write the HTML back to the browser.
        out.println("<html>");
        out.println("<body>");

        out.println("You should never see this");
        out.println("because my output buffer gets erased");

// Get the request dispatcher for the JSP to include.
        RequestDispatcher dispatcher =
            request.getRequestDispatcher(
                "/ForwardedPage.jsp");

        dispatcher.forward(request, response);

        out.println("</body>");
        out.println("</html>");
    }
}

Passing Parameters to the Forwarded Page

As you might have already guessed, you pass parameters to a forwarded page in exactly the same way as you would to an included page. From a JavaServer Page, you use the <jsp:param> tag, and from a servlet, you add the parameters to the end of the URL when you call getRequestDispatcher.

    [ Team LiB ] Previous Section Next Section