⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servlets7.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<?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>Writing Service Methods</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="Servlets6.html" />    <link rel="Next" href="Servlets8.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="Servlets6.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="Servlets8.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="wp64424"> </a><h2 class="pHeading1">Writing Service Methods</h2><a name="wp64425"> </a><p class="pBody">The service provided by a servlet is implemented in the <code class="cCode">service</code> method of a <code class="cCode">GenericServlet</code>, the <code class="cCode">do</code><code class="cVariable">Method</code> methods (where <code class="cVariable">Method</code> can take the value <code class="cCode">Get</code>, <code class="cCode">Delete</code>, <code class="cCode">Options</code>, <code class="cCode">Post</code>, <code class="cCode">Put</code>, <code class="cCode">Trace</code>) of an <code class="cCode">HttpServlet</code>, or any other protocol-specific methods defined by a class that implements the <code class="cCode">Servlet</code> interface. In the rest of this chapter, the term <em class="cEmphasis">service method</em> will be used for any method in a servlet class that provides a service to a client.</p><a name="wp64429"> </a><p class="pBody">The general pattern for a service method is to extract information from the request, access external resources, and then populate the response based on that information. </p><a name="wp75688"> </a><p class="pBody">For HTTP servlets, the correct procedure for populating the response is to first retrieve an output stream from the response, then fill in the response headers, and finally write any body content to the output stream. Response headers must always be set before the response has been committed. Any attempt to set/add headers after the response has been committed will be ignored by the Web container. The next two sections describe how to get information from requests and generate responses.</p><a name="wp64433"> </a><h3 class="pHeading2">Getting Information from Requests</h3><a name="wp64435"> </a><p class="pBody">A request contains data passed between a client and the servlet. All requests implement the <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html" target="_blank">ServletRequest</a></code> interface. This interface defines methods for accessing the following information:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64439"> </a><div class="pSmartList1"><li>Parameters, which are typically used to convey information between clients and servlets</li></div><a name="wp64440"> </a><div class="pSmartList1"><li>Object-valued attributes, which are typically used to pass information between the servlet container and a servlet or between collaborating servlets</li></div><a name="wp64441"> </a><div class="pSmartList1"><li>Information about the protocol used to communicate the request and the client and server involved in the request</li></div><a name="wp64442"> </a><div class="pSmartList1"><li>Information relevant to localization</li></div></ul></div><a name="wp64444"> </a><p class="pBody">For example, in <code class="cCode"><a  href="../examples/web/bookstore1/src/servlets/CatalogServlet.java" target="_blank">CatalogServlet</a></code> the identifier of the book that a customer wishes to purchase is included as a parameter to the request. The following code fragment illustrates how to use the <code class="cCode">getParameter</code> method to extract the identifier:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">String bookId = request.getParameter(&quot;Add&quot;);if (bookId != null) {&nbsp;&nbsp;BookDetails book = bookDB.getBookDetails(bookId);<a name="wp64446"> </a></pre></div><a name="wp64447"> </a><p class="pBody">You can also retrieve an input stream from the request and manually parse the data. To read character data, use the <code class="cCode"><a  href="http://java.sun.com/j2se/1.4/docs/api/java/io/BufferedReader.html" target="_blank">BufferedReader</a></code> object returned by the request's <code class="cCode">getReader</code> method. To read binary data, use the <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletInputStream.html" target="_blank">ServletInputStream</a></code> returned by <code class="cCode">getInputStream</code>. </p><a name="wp75969"> </a><p class="pBody">HTTP servlets are passed an HTTP request object, <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html" target="_blank">HttpServletRequest</a></code>, which contains the request URL, HTTP headers, query string, and so on.</p><a name="wp75971"> </a><p class="pBody">An HTTP request URL contains the following parts:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">http://[host]:[port][request path]?[query string]<a name="wp64460"> </a></pre></div><a name="wp64461"> </a><p class="pBody">The request path is further composed of the following elements:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64462"> </a><div class="pSmartList1"><li><span style="font-weight: bold">Context path:</span> A concatenation of a forward slash <code class="cCode">/</code> with the context root of the servlet's Web application.</li></div><a name="wp64463"> </a><div class="pSmartList1"><li><span style="font-weight: bold">Servlet path:</span> The path section that corresponds to the component alias that activated this request. This path starts with a forward slash <code class="cCode">/</code>.</li></div><a name="wp64464"> </a><div class="pSmartList1"><li><span style="font-weight: bold">Path info:</span> The part of the request path that is not part of the context path or the servlet path.</li></div></ul></div><a name="wp64468"> </a><p class="pBody">If the context path is <code class="cCode">/catalog</code> and for the aliases listed in <a  href="Servlets7.html#wp64478">Table 11-5</a>, <a  href="Servlets7.html#wp64499">Table 11-6</a> gives some examples of how the URL will be parsed.</p><div align="left"><table border="1" summary="Component Aliases" id="wp64478">  <caption><a name="wp64478"> </a><div class="pTableTitle">Table 11-5   Aliases&nbsp;</div></caption>  <tr align="center">    <th><a name="wp64482"> </a><div class="pCellHeading">Pattern</div></th>    <th><a name="wp64484"> </a><div class="pCellHeading">Servlet</div></th></tr>  <tr align="left">    <td><a name="wp64486"> </a><div class="pCellBody"><code class="cCode">/lawn/*</code></div></td>    <td><a name="wp64488"> </a><div class="pCellBody"><code class="cCode">LawnServlet</code></div></td></tr>  <tr align="left">    <td><a name="wp64490"> </a><div class="pCellBody"><code class="cCode">/*.jsp</code></div></td>    <td><a name="wp64492"> </a><div class="pCellBody"><code class="cCode">JSPServlet</code></div></td></tr></table></div><p class="pBody"></p><div align="left"><table border="1" summary="Request Path Elements" id="wp64499">  <caption><a name="wp64499"> </a><div class="pTableTitle">Table 11-6   Request Path Elements&nbsp;</div></caption>  <tr align="center">    <th><a name="wp64505"> </a><div class="pCellHeading">Request Path</div></th>    <th><a name="wp64507"> </a><div class="pCellHeading">Servlet Path</div></th>    <th><a name="wp64509"> </a><div class="pCellHeading">Path Info</div></th></tr>  <tr align="left">    <td><a name="wp64511"> </a><div class="pCellBody"><code class="cCode">/catalog/lawn/index.html</code></div></td>    <td><a name="wp64513"> </a><div class="pCellBody">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -