Start of Content Area

Process documentation Dispatching Requests to Web Application Resources  Locate the document in its SAP Library structure

Purpose

Servlets can invoke other resources of the Web application, such as other servlets, JSP pages, or HTML pages, both directly and indirectly. The resource is invoked indirectly when the servlet sends a redirect URL in the response to the client.

A servlet invokes another servlet, for example, by forwarding the request to it, or by including the content of that servlet in the response it generates. You can use the RequestDispatcher object and its include and forward methods to program your servlets to dispatch requests to other Web application resources.

Process Flow

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:

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

// Get ServletContect object

ServletContext sc = getServletConfig().getServletContext();

// Obtain the RequestDispatcher object by the path to the

// resource

RequestDispatcher rd = sc.getRequestDispatcher(path);

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.

                            b.      Obtain the RequestDispatcher from the request object:

RequestDispatcher rd = request.getRequestDispatcher(path);

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.

       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.

 

 

End of Content Area