Show TOC

Background documentationForwarding Requests Locate this document in the navigation structure

 

You forward requests to be processed by another Web resource using the forward() method of the RequestDispatcher object. You may use request forwarding if you want to have a servlet that does some preliminary processing of the request, and then to forward it to another resource to generate a response and send it to the client. This is the major difference between the forward and the include requests - when you use forward() method, the content of the response that the first component has generated is ignored, and the second component generates the whole response and sends it to the client.

Restriction When Forwarding Requests

There is a restriction when using forward() method that you must observe in your servlet code. You must not have sent any content of the response in order to forward a request. If the response buffer of the initial servlet contains any response content, it is discarded.

Security Checks

If you use authentication for the resources of your Web application, it does not force a new security check when you forward a request to a protected resource.

Calling getRequestURL() Method for Forwarded Requests

If a request is forwarded to a servlet or JSP, and this servlet or JSP calls the HttpServletRequest.getRequestURL() method, then the URL of the current servet or JSP is returned. For example, if the client requests /A.jsp, and A.jsp forwards the request to /B.jsp, then calling the getRequestURL() method within the B.jsp returns /B.jsp. This is the default behavior of the getRequestURL() method.

There is an option to change this behavior so that method returns the URL of the servlet or JSP that was originally requested by the client. Using the above example of A.jsp forwarding the request to B.jsp, if you change the method's behavior, it will return /A.jsp as a result.

Client Caching in Case of Forward Requests to Static HTTP Resources

If you forward a request to a static resource in your servlet or JSP code, then the AS Java treats the resource returned with the response as a dynamic HTTP resource. Accordingly, the AS Java sets the appropriate headers to the response to prevent the resource from being cached in client caches.

More information about configuring caching of HTTP responses on the client side: Configuring HTTP Responses Caching by Client Caches.

If you want to force the AS Java to treat the response to the forwarded request as a static one (and to set the cache controlling headers accordingly), you need to set a specific attribute to the request before you actually forward it in your servlet code. The name of this attribute is com.sap.engine.servlets_jsp.forward.static.set-cache-headers.

Example Example

The following code excerpt shows how to forward the request to a plain text file so that the AS Java sets the cache controlling headers in accordance with the rules configured for static HTTP responses:

  1. request.setAttribute("com.sap.engine.servlets_jsp.forward.static.set-cache-headers", new Boolean(true));
    application.getRequestDispatcher("/test.txt").forward(request,response);
    
End of the code.