The resource that a client requests is sometimes not the resource that the server serves up. The following are examples of when the server responding with a request not explicitly made by a client:
Use the following ways to pass control of a request from one servlet to another:
response.sendRedirect method
When you use the forward method, the calling servlet cannot write to the output stream. If necessary, the calling servlet can pass information to the target servlet by setting attributes using the ServletRequest object's setAttribute method (request object in JSP). Then the target program can access attributes as follows:
getAttribute method.
getAttribute method.
You can use the RequestDispatcher object's forward method to pass control to another servlet or a JSP. This section describes how to do this.
You obtain a reference to the RequestDispatcher object through the getRequestDispatcher method, which is found in both the ServletContext object and the ServletRequest object. The only difference is that the pathname specified in ServletRequest.getRequestDispatcher does not require a leading slash, so you can use it with a relative URL. ServletContext.getRequestDispatcher requires a leading slash. The following examples use ServletContext.getRequestDispatcher.
The following example passes control to another servlet:
...
ServletContext sc = this.getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/servlet/callMe");
if (rd !=null) {
��// Pass control to the servlet.
��try {
����rd.forward(req, resp);
��} catch (Exception e) {
����sc.log("Problem invoking servlet.", e);
��}
}
...
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.
You can use the RequestDispatcher object to pass control from a servlet to a JSP. You can also use this technique to pass control to an HTML page or other web resource.
The following example passes control to a JSP:
...
ServletContext sc = this.getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/test.jsp");
if (rd !=null) {
��// Pass control to the JSP.
��try {
����rd.forward(req, resp);
��} catch (Exception e) {
����sc.log("Problem invoking JSP.", e);
��}
}
...
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.
The response object includes the sendRedirect method, which lets you forward a request to a new target. Under the covers, the sendRedirect method adds a Location HTTP header to the response, as well as changes the status code to 302 (Found). You can point the request to a relative or absolute URL as the location. If you provide a relative URL, JRun converts it to an absolute URL using the current URI before generating the response.
The signature of the sendRedirect method is as follows:
public void sendRedirect(String URL)
The following servlet example gets a location from the target_location request parameter and then redirects the request to that location:
...
String target_location = req.getParameter("target_location");
resp.sendRedirect(target_location);
...
The value of target_location can be any String from a URL to the name of another servlet; for example:
Location header equal to it.
Consider the following caveats when using the sendRedirect method:
sendRedirect when processing GET requests.Referer header to find out where the request came from after it is redirected.sendRedirect method, ensure that you encode the URL before invoking the method.You can view the headers that JRun sets for the request and response messages using the TCPMonitor utility. For more information on using TCPMonitor, see "Using TCPMonitor".
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/techniques_servlet17.htm