Show TOC

Procedure documentationDispatching Requests to Web Application Resources Locate this document in the navigation structure

 

If necessary, servlets and JSP components can redirect client requests to other servlet and JSP components. This can be done in two ways:

  • Server-side forwarding

    This process occurs on the server and is transparent to the user. In this case, the servlet or JSP directly invokes another servlet or JSP component, and passes to it the client request. The request is preserved in its original state (as received), and the servlet or JSP can process the request with all its original attributes.

    You do server-side forward by using the forward() method of RequestDispatcher.

  • Client-side redirecting

    This process occurs on the client, therefore it is not transparent to the user. In this case, the servlet or JSP sends a response to the user agent (Web browser) to redirect the client request to a new servlet or JSP or resource, which means that the browser sends a new request to the new resource. The browser history holds the URL of both the original request, and the new redirected request. The original request and the redirected request are different, however, so the original request attributes are not preserved in the redirected request. As a workaround, you can add the necessary attributes as parameters in the request URL.

    You perform a client-side redirect by using the redirect method of the HTTP response object.

The following list summarizes the main differences between the two approaches:

  • All other conditions being equal, a server-side forward will be faster than a client-side redirect

  • A forward allows you access to the original HTTP request object. A redirect does not.

  • The URL in the browser will change on a redirect. It will remain the same on a forward.

Procedure

The process of programming a servlet to dispatch requests consists of two major steps:

  1. Obtain the RequestDispatcher object. You have several options for doing this:

    1. Obtain the RequestDispatcher from the ServletContext. You must first get the ServletContext object:

      // Get ServletContect objectServletContext sc = getServletConfig().getServletContext(); // Obtain the RequestDispatcher object by the path to the // resource RequestDispatcher rd = sc.getRequestDispatcher(path);

      Note Note

      You can also obtain it using the getNamedDispatcher(name) method of the ServletContext object. In this case, the parameter name is the name of the servlet as declared in the <servlet-name> tag of the deployment descriptor of your Web application.

      End of the note.
    2. Obtain the RequestDispatcher from the request object:

      RequestDispatcher rd = request.getRequestDispatcher(path);

      Note Note

      There is a difference between the value of the path parameter when obtaining the RequestDispatcher from the servlet context and the request. In the first case, the path is relative to the Web application's root. In the second case, the path is relative to the current request.

      End of the note.
  2. Dispatch the request to another resource using either include() or forward() methods of the RequestDispatcher:

    rd.include(request, response);

    or

    rd.forward(request, response);

For more details on requests dispatching topic, refer to the Java Servlet 2.3 Specification.