📄 servlets9.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Invoking Other Web Resources</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="Servlets8.html" /> <link rel="Next" href="Servlets10.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="Servlets8.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="Servlets10.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp64684"> </a><h2 class="pHeading1">Invoking Other Web Resources</h2><a name="wp64686"> </a><p class="pBody">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, <code class="cCode">ShowCartServlet</code> indirectly invokes the <code class="cCode">CatalogServlet</code> through the embedded URL <code class="cCode">/bookstore1/catalog</code>. </p><a name="wp64688"> </a><p class="pBody">A Web component can also directly invoke another resource while it is executing. There are two possibilities: it can include the content of another resource, or it can forward a request to another resource.</p><a name="wp64689"> </a><p class="pBody">To invoke a resource available on the server that is running a Web component, you must first obtain a <code class="cCode"><a href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/RequestDispatcher.html" target="_blank">RequestDispatcher</a></code> object using the <code class="cCode">getRequestDispatcher("URL")</code> method.</p><a name="wp64693"> </a><p class="pBody">You can get a <code class="cCode">RequestDispatcher</code> 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 <code class="cCode">/</code>), but the Web context requires an absolute path. If the resource is not available, or if the server has not implemented a <code class="cCode">RequestDispatcher</code> object for that type of resource, <code class="cCode">getRequestDispatcher</code> will return null. Your servlet should be prepared to deal with this condition.</p><a name="wp64695"> </a><h3 class="pHeading2">Including Other Resources in the Response</h3><a name="wp64697"> </a><p class="pBody">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 <code class="cCode">include</code> method of a <code class="cCode">RequestDispatcher </code>object:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">include(request, response);<a name="wp64699"> </a></pre></div><a name="wp64700"> </a><p class="pBody">If the resource is static, the <code class="cCode">include</code> 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:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64701"> </a><div class="pSmartList1"><li>It can write to the body of the response and commit a response.</li></div><a name="wp64702"> </a><div class="pSmartList1"><li>It cannot set headers or call any method (for example, <code class="cCode">setCookie</code>) that affects the headers of the response.</li></div></ul></div><a name="wp64704"> </a><p class="pBody">The banner for the Duke's Bookstore application is generated by <code class="cCode"><a href="../examples/web/bookstore1/src/servlets/BannerServlet.java" target="_blank">BannerServlet</a></code>. Note that both the <code class="cCode">doGet</code> and <code class="cCode">doPost</code> methods are implemented because <code class="cCode">BannerServlet</code> can be dispatched from either method in a calling servlet.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class BannerServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<body bgcolor=\"#ffffff\">" + "<center>" + "<hr> <br> &nbsp;" + "<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> &nbsp; <hr> <br> "); } public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<body bgcolor=\"#ffffff\">" + "<center>" + "<hr> <br> &nbsp;" + "<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> &nbsp; <hr> <br> "); }}<a name="wp64705"> </a></pre></div><a name="wp64706"> </a><p class="pBody">Each servlet in the Duke's Bookstore application includes the result from <code class="cCode">BannerServlet</code> with the following code:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/banner");if (dispatcher != null) dispatcher.include(request, response);} <a name="wp64707"> </a></pre></div><a name="wp64709"> </a><h3 class="pHeading2">Transferring Control to Another Web Component</h3><a name="wp64710"> </a><p class="pBody">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. </p><a name="wp66365"> </a><p class="pBody">To transfer control to another Web component, you invoke the <code class="cCode">forward</code> method of a <code class="cCode">RequestDispatcher</code>. When a request is forwarded, the request URI is set to the path of the forwarded page. The original URI and its constituent parts are saved as a request attributes <code class="cCode">javax.servlet.forward.[request_uri|context-path|servlet_path|path_info|query_string]</code>. The <code class="cCode"><a href="../examples/web/bookstore2/src/Dispatcher.java" target="_blank">Dispatcher</a></code> servlet, used by a version of the Duke's Bookstore application described in <a href="JSPTags3.html#wp89456">The Example JSP Pages</a>, saves the path information from the original URL, retrieves a <code class="cCode">RequestDispatcher</code> from the request, and then forwards to the JSP page <code class="cCode"><a href="../examples/web/bookstore3/web/template/template.txt" target="_blank">template.jsp</a></code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">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, ...}<a name="wp64719"> </a></pre></div><a name="wp64720"> </a><p class="pBody">The <code class="cCode">forward</code> method should be used to give another resource responsibility for replying to the user. If you have already accessed a <code class="cCode">ServletOutputStream</code> or <code class="cCode">PrintWriter</code> object within the servlet, you cannot use this method; it throws an <code class="cCode">IllegalStateException</code>.</p> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="Servlets8.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="Servlets10.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -