Download
FAQ History |
API
Search Feedback |
Invoking Other Web Resources
Web components can invoke other Web resources in two ways: indirectly and directly. A Web component indirectly invokes another Web resource when it embeds a URL that points to another Web component in content returned to a client. In the Duke's Bookstore application, most Web components contain embedded URLs that point to other Web components. For example,
ShowCartServlet
indirectly invokes theCatalogServlet
through the embedded URL/bookstore1/catalog
.A Web component can also directly invoke another resource while it is executing. There are two possibilities: The Web component can include the content of another resource, or it can forward a request to another resource.
To invoke a resource available on the server that is running a Web component, you must first obtain a
RequestDispatcher
object using thegetRequestDispatcher("URL")
method.You can get a
RequestDispatcher
object from either a request or the Web context; however, the two methods have slightly different behavior. The method takes the path to the requested resource as an argument. A request can take a relative path (that is, one that does not begin with a/
), but the Web context requires an absolute path. If the resource is not available or if the server has not implemented aRequestDispatcher
object for that type of resource,getRequestDispatcher
will return null. Your servlet should be prepared to deal with this condition.Including Other Resources in the Response
It is often useful to include another Web resource--for example, banner content or copyright information--in the response returned from a Web component. To include another resource, invoke the
include
method of aRequestDispatcher
object:If the resource is static, the
include
method enables programmatic server-side includes. If the resource is a Web component, the effect of the method is to send the request to the included Web component, execute the Web component, and then include the result of the execution in the response from the containing servlet. An included Web component has access to the request object, but it is limited in what it can do with the response object:The banner for the Duke's Bookstore application is generated by
BannerServlet
. Note that bothdoGet
anddoPost
are implemented becauseBannerServlet
can be dispatched from either method in a calling servlet.public class BannerServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { output(request, response); } public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { output(request, response); } private void output(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<body bgcolor=\"#ffffff\">" + "<center>" + "<hr> <br> " + "<h1>" + "<font size=\"+3\" color=\"#CC0066\">Duke's </font>" + <img src=\"" + request.getContextPath() + "/duke.books.gif\">" + "<font size=\"+3\" color=\"black\">Bookstore</font>" + "</h1>" + "</center>" + "<br> <hr> <br> "); } }Each servlet in the Duke's Bookstore application includes the result from
BannerServlet
using the following code:RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/banner"); if (dispatcher != null) dispatcher.include(request, response); }Transferring Control to Another Web Component
In some applications, you might want to have one Web component do preliminary processing of a request and have another component generate the response. For example, you might want to partially process a request and then transfer to another component depending on the nature of the request.
To transfer control to another Web component, you invoke the
forward
method of aRequestDispatcher
. When a request is forwarded, the request URL is set to the path of the forwarded page. The original URI and its constituent parts are saved as request attributesjavax.servlet.forward.[request_uri|context-path|servlet_path|path_info|query_string]
. TheDispatcher
servlet, used by a version of the Duke's Bookstore application described in The Example JSP Pages, saves the path information from the original URL, retrieves aRequestDispatcher
from the request, and then forwards to the JSP pagetemplate.jsp
.public class Dispatcher extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { RequestDispatcher dispatcher = request. getRequestDispatcher("/template.jsp"); if (dispatcher != null) dispatcher.forward(request, response); } public void doPost(HttpServletRequest request, ... }The
forward
method should be used to give another resource responsibility for replying to the user. If you have already accessed aServletOutputStream
orPrintWriter
object within the servlet, you cannot use this method; doing so throws anIllegalStateException
.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.