Show TOC Start of Content Area

Background documentation Forwarding Requests  Locate the document in its SAP Library structure

Overview

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 HttpRequest.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.jspforwarding the request to B.jsp, if you change the method’s behavior, it will return /A.jsp as a result.

You can change the default behavior of the getRequestURL() method using the <get-request-url-mode> property in the additional Web deployment descriptor (web-j2ee-engine.xml) at deployment time. If the value of the property is REQUEST-DISPATCHER, then the default behavior is in place. To change it so that the getRequestURL() method returns the URL of the originally requested servlet or JSP, you need to set CLIENT as the property value.

Recommendation

SAP recommends that you use the default behavior of the getRequestURL() method in cases of forwarded requests. The other option is provided only to enable support for applications that are being ported to J2EE Engine from other J2EE application servers that implement different behavior of the same method.

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 J2EE Engine treats the resource returned with the response as a dynamic HTTP resource. Accordingly, the J2EE Engine sets the appropriate headers to the response to prevent the resource from being cached in client caches. For more information about configuring caching of HTTP responses on the client side, see Configuring HTTP Responses Caching by Client Caches.

If you want to force the J2EE Engine 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

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

request.setAttribute("com.sap.engine.servlets_jsp.forward.static.set-cache-headers", new Boolean(true));

application.getRequestDispatcher("/test.txt").forward(request,response);

 

End of Content Area