📄 servlets7.html
字号:
<code class="cCode">/lawn</code></div></td> <td><a name="wp64515"> </a><div class="pCellBody"><code class="cCode">/index.html</code></div></td></tr> <tr align="left"> <td><a name="wp64517"> </a><div class="pCellBody"><code class="cCode">/catalog/help/feedback.jsp</code></div></td> <td><a name="wp64519"> </a><div class="pCellBody"><code class="cCode">/help/feedback.jsp</code></div></td> <td><a name="wp64521"> </a><div class="pCellBody"><code class="cCode">null</code></div></td></tr></table></div><p class="pBody"></p><a name="wp64523"> </a><p class="pBody">Query strings are composed of a set of parameters and values. Individual parameters are retrieved from a request with the <code class="cCode">getParameter</code> method. There are two ways to generate query strings:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64524"> </a><div class="pSmartList1"><li>A query string can explicitly appear in a Web page. For example, an HTML page generated by the <code class="cCode"><a href="../examples/web/bookstore1/src/servlets/CatalogServlet.java" target="_blank">CatalogServlet</a></code> could contain the link <code class="cCode"><a href="/bookstore1/catalog?Add=101">Add To Cart</a></code>. <code class="cCode">CatalogServlet</code> extracts the parameter named <code class="cCode">Add</code> as follows:</li></div><a name="wp64526"> </a><p class="pBodyRelative"><code class="cCode"> String bookId = request.getParameter("Add");</code></p><a name="wp64527"> </a><div class="pSmartList1"><li>A query string is appended to a URL when a form with a <code class="cCode">GET</code> HTTP method is submitted. In the Duke's Bookstore application, <code class="cCode"><a href="../examples/web/bookstore1/src/servlets/CashierServlet.java" target="_blank">CashierServlet</a></code> generates a form, then a user name input to the form is appended to the URL that maps to <code class="cCode"><a href="../examples/web/bookstore1/src/servlets/ReceiptServlet.java" target="_blank">ReceiptServlet</a></code>, and finally <code class="cCode">ReceiptServlet</code> extracts the user name using the <code class="cCode">getParameter</code> method.</li></div></ul></div><a name="wp64531"> </a><h3 class="pHeading2">Constructing Responses</h3><a name="wp64533"> </a><p class="pBody">A response contains data passed between a server and the client. All responses implement the <code class="cCode"><a href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletResponse.html" target="_blank">ServletResponse</a></code> interface. This interface defines methods that allow you to do the following:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64537"> </a><div class="pSmartList1"><li>Retrieve an output stream to use to send data to the client. To send character data, use the <code class="cCode"><a href="http://java.sun.com/j2se/1.4/docs/api/java/io/PrintWriter.html" target="_blank">PrintWriter</a></code> returned by the response's <code class="cCode">getWriter</code> method. To send binary data in a MIME body response, use the <code class="cCode"><a href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletOutputStream.html" target="_blank">ServletOutputStream</a></code> returned by <code class="cCode">getOutputStream</code>. To mix binary and text data, for example, to create a multipart response, use a <code class="cCode">ServletOutputStream</code> and manage the character sections manually. </li></div><a name="wp64543"> </a><div class="pSmartList1"><li>Indicate the content type (for example, <code class="cCode">text/html</code>), being returned by the response with the <code class="cCode">setContentType(String)</code> method. This method must be called before the response is committed. A registry of content type names is kept by the Internet Assigned Numbers Authority (IANA) at: </li></div><a name="wp64545"> </a><p class="pBodyRelative"><code class="cCode"><a href="http://www.iana.org/assignments/media-types/" target="_blank">http://www.iana.org/assignments/media-types/</a></code></p><a name="wp75717"> </a><div class="pSmartList1"><li>Indicate whether to buffer output with the <code class="cCode">setBufferSize(int)</code> method. By default, any content written to the output stream is immediately sent to the client. Buffering allows content to be written before anything is actually sent back to the client, thus providing the servlet with more time to set appropriate status codes and headers or forward to another Web resource. The method must be called before any content is written or the response is committed.</li></div><a name="wp64548"> </a><div class="pSmartList1"><li>Set localization information such as locale and character encoding. See Chapter <a href="WebI18N.html#wp83291">17</a> for details.</li></div></ul></div><a name="wp64552"> </a><p class="pBody">HTTP response objects, <code class="cCode"><a href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletResponse.html" target="_blank">HttpServletResponse</a></code>, have fields representing HTTP headers such as</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp66332"> </a><div class="pSmartList1"><li>Status codes, which are used to indicate the reason a request is not satisfied or that a request has been redirected. </li></div><a name="wp66333"> </a><div class="pSmartList1"><li>Cookies, which are used to store application-specific information at the client. Sometimes cookies are used to maintain an identifier for tracking a user's session (see <a href="Servlets11.html#wp64784">Session Tracking</a>).</li></div></ul></div><a name="wp64559"> </a><p class="pBody">In Duke's Bookstore, <code class="cCode"><a href="../examples/web/bookstore1/src/servlets/BookDetailsServlet.java" target="_blank">BookDetailsServlet</a></code> generates an HTML page that displays information about a book that the servlet retrieves from a database. The servlet first sets response headers: the content type of the response and the buffer size. The servlet buffers the page content because the database access can generate an exception that would cause forwarding to an error page. By buffering the response, the client will not see a concatenation of part of a Duke's Bookstore page with the error page should an error occur. The <code class="cCode">doGet</code> method then retrieves a <code class="cCode">PrintWriter</code> from the response. </p><a name="wp64560"> </a><p class="pBody">For filling in the response, the servlet first dispatches the request to <code class="cCode">BannerServlet</code>, which generates a common banner for all the servlets in the application. This process is discussed in <a href="Servlets9.html#wp64695">Including Other Resources in the Response</a>. Then the servlet retrieves the book identifier from a request parameter and uses the identifier to retrieve information about the book from the bookstore database. Finally, the servlet generates HTML markup that describes the book information and commits the response to the client by calling the <code class="cCode">close</code> method on the <code class="cCode">PrintWriter</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class BookDetailsServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set headers before accessing the Writer response.setContentType("text/html"); response.setBufferSize(8192); PrintWriter out = response.getWriter(); // then write the response out.println("<html>" + "<head><title>+ messages.getString("TitleBookDescription") +</title></head>"); // Get the dispatcher; it gets the banner to the user RequestDispatcher dispatcher = getServletContext(). getRequestDispatcher("/banner"); if (dispatcher != null) dispatcher.include(request, response); //Get the identifier of the book to display String bookId = request.getParameter("bookId"); if (bookId != null) { // and the information about the book try { BookDetails bd = bookDB.getBookDetails(bookId); ... //Print out the information obtained out.println("<h2>" + bd.getTitle() + "</h2>" + ... } catch (BookNotFoundException ex) { response.resetBuffer(); throw new ServletException(ex); } } out.println("</body></html>"); out.close(); }}<a name="wp64564"> </a></pre></div><a name="wp64565"> </a><p class="pBody"><code class="cCode">BookDetailsServlet</code> generates a page that looks like:</p><a name="wp64569"> </a><p class="pBody"></p><div align="left"><img src="images/Fig242.gif" height="360" width="356" alt=" Bppk Details Screen" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p> <a name="64570"> </a><strong><font >Figure 11-2 Book Details</font></strong></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="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"><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 + -