Forwarding a Request
A servlet or filter can forward a request to a different filter or servlet in the same container, or to any HTTP URL. For example, the filter code below forwards a request to a servlet named ServletTwo.
rwsf::ServletContext context =
// Dispatch to a servlet by name
rwsf::RequestDispatcher nameRD =
nameRD.forward(request, response);
Requests forwarded within a container bypass any filter chain for the destination. So, in the example above, if ServletTwo relies on a filter to uncompress the payload of the request, that filter will not be called when the request is forwarded.
A request dispatcher can target any HTTP URL reachable by the container. The following servlet code forwards a request to a remote Web page.
rwsf::ServletContext context = getServletContext();
// Dispatch to a remote URL
rwsf::RequestDispatcher locationRD =
locationRD.forward(request, response);
Dispatching a request to an HTTP URL does not redirect the client to the remote Web page. Instead, the servlet container retrieves the remote Web page and returns the Web page to the client. The client has no knowledge of the original source of the Web page. In general, this means that a client will not be able to load images from the returned Web page, that relative links reference the container rather than the original resource, and so on. This technique is useful for dispatching Web service requests to another machine for processing.