📄 servlets3.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>The Example Servlets</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="Servlets2.html" /> <link rel="Next" href="Servlets4.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="Servlets2.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="Servlets4.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="wp63984"> </a><h2 class="pHeading1">The Example Servlets</h2><a name="wp63985"> </a><p class="pBody">This chapter uses the Duke's Bookstore application to illustrate the tasks involved in programming servlets. <a href="Servlets3.html#wp63995">Table 11-1</a> lists the servlets that handle each bookstore function. Each programming task is illustrated by one or more servlets. For example, <code class="cCode">BookDetailsServlet</code> illustrates how to handle HTTP <code class="cCode">GET</code> requests, <code class="cCode">BookDetailsServlet</code> and <code class="cCode">CatalogServlet</code> show how to construct responses, and <code class="cCode">CatalogServlet</code> illustrates how to track session information.</p><div align="left"><table border="1" summary="Duke's Bookstore Servlets" id="wp63995"> <caption><a name="wp63995"> </a><div class="pTableTitle">Table 11-1 Duke's Bookstore Example Servlets </div></caption> <tr align="center"> <th><a name="wp63999"> </a><div class="pCellHeading">Function</div></th> <th><a name="wp64001"> </a><div class="pCellHeading">Servlet</div></th></tr> <tr align="left"> <td><a name="wp64003"> </a><div class="pCellBody">Enter the bookstore</div></td> <td><a name="wp64005"> </a><div class="pCellBody"><code class="cCode">BookStoreServlet</code></div></td></tr> <tr align="left"> <td><a name="wp64007"> </a><div class="pCellBody">Create the bookstore banner</div></td> <td><a name="wp64009"> </a><div class="pCellBody"><code class="cCode">BannerServlet</code></div></td></tr> <tr align="left"> <td><a name="wp64011"> </a><div class="pCellBody">Browse the bookstore catalog</div></td> <td><a name="wp64013"> </a><div class="pCellBody"><code class="cCode">CatalogServlet</code></div></td></tr> <tr align="left"> <td><a name="wp64015"> </a><div class="pCellBody">Put a book in a shopping cart</div></td> <td><a name="wp64017"> </a><div class="pCellBody"><code class="cCode">CatalogServlet</code>,</div><a name="wp64018"> </a><div class="pCellBody"><code class="cCode">BookDetailsServlet</code></div></td></tr> <tr align="left"> <td><a name="wp64020"> </a><div class="pCellBody">Get detailed information on a specific book</div></td> <td><a name="wp64022"> </a><div class="pCellBody"><code class="cCode">BookDetailsServlet</code></div></td></tr> <tr align="left"> <td><a name="wp64024"> </a><div class="pCellBody">Display the shopping cart</div></td> <td><a name="wp64026"> </a><div class="pCellBody"><code class="cCode">ShowCartServlet</code></div></td></tr> <tr align="left"> <td><a name="wp64028"> </a><div class="pCellBody">Remove one or more books from the shopping cart</div></td> <td><a name="wp64030"> </a><div class="pCellBody"><code class="cCode">ShowCartServlet</code></div></td></tr> <tr align="left"> <td><a name="wp64032"> </a><div class="pCellBody">Buy the books in the shopping cart</div></td> <td><a name="wp64034"> </a><div class="pCellBody"><code class="cCode">CashierServlet</code></div></td></tr> <tr align="left"> <td><a name="wp64036"> </a><div class="pCellBody">Receive an acknowledgement for the purchase</div></td> <td><a name="wp64038"> </a><div class="pCellBody"><code class="cCode">ReceiptServlet</code></div></td></tr></table></div><p class="pBody"></p><a name="wp64039"> </a><p class="pBody">The data for the bookstore application is maintained in a database and accessed through the helper class <code class="cCode">database.BookDB</code>. The <code class="cCode">database</code> package also contains the class <code class="cCode">BookDetails</code>, which represents a book. The shopping cart and shopping cart items are represented by the classes <code class="cCode">cart.ShoppingCart</code> and <code class="cCode">cart.ShoppingCartItem</code>, respectively.</p><a name="wp68429"> </a><p class="pBody">The source code for the bookstore application is located in the <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/web/bookstore1/</code> directory created when you unzip the tutorial bundle (see <a href="About.html#wp67678">Building and Running the Examples</a>). A sample <code class="cCode">bookstore1.war</code> is provided in <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/web/provided-wars/</code>. To build, package, deploy, and run the example with <code class="cCode">deploytool</code>:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp76599"> </a><div class="pSmartList1"><li>Build and package the bookstore common files as described in <a href="WebApp12.html#wp189559">Duke's Bookstore Examples</a>. </li></div><a name="wp76602"> </a><div class="pSmartList1"><li>In a terminal window, go to<code class="cCode"> <</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/web/bookstore1/</code>.</li></div><a name="wp76292"> </a><div class="pSmartList1"><li>Run <code class="cCode">asant</code> <code class="cCode">build</code>. This target will spawn any necessary compilations and copy files to the <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/web/bookstore1/build/</code> directory. </li></div><a name="wp76293"> </a><div class="pSmartList1"><li>Start the J2EE application server.</li></div><a name="wp76296"> </a><div class="pSmartList1"><li>Perform all the operations described in <a href="WebApp13.html#wp69868">Accessing Databases from Web Applications</a>.</li></div><a name="wp81233"> </a><div class="pSmartList1"><li>Start <code class="cCode">deploytool</code>.</li></div><a name="wp81234"> </a><div class="pSmartList1"><li>Create a Web application called <code class="cCode">bookstore1</code> by running the New Web Component Wizard. Select File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>New<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Web Component.</li></div><a name="wp81235"> </a><div class="pSmartList1"><li>New Web Component Wizard</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81236"> </a><div class="pSmartList2"><li>Select the Create New Stand-Alone WAR Module radio button.</li></div><a name="wp81237"> </a><div class="pSmartList2"><li>In the WAR Location field, enter <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/docs/tutorial/examples/web/bookstore1/build/bookstore1.war</code>. </li></div><a name="wp81238"> </a><div class="pSmartList2"><li>In the WAR Name field, enter <code class="cCode">bookstore1</code>.</li></div><a name="wp81239"> </a><div class="pSmartList2"><li>In the Context Root field, enter <code class="cCode">/bookstore1</code>.</li></div><a name="wp81240"> </a><div class="pSmartList2"><li>Click Edit.</li></div><a name="wp81241"> </a><div class="pSmartList2"><li>In the Edit Archive Contents dialog box, navigate to<code class="cCode"> <</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/web/bookstore1/build/</code>. Select <code class="cCode">errorpage.html</code>, <code class="cCode">duke.books.gif</code>, and the <code class="cCode">servlets</code>, <code class="cCode">database</code>, <code class="cCode">filters</code>, <code class="cCode">listeners</code>, and <code class="cCode">util</code> packages. Click Add.</li></div><a name="wp81242"> </a><div class="pSmartList2"><li>Add the shared bookstore library. Navigate to<code class="cCode"> <</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/build/web/bookstore/dist/</code>. Select <code class="cCode">bookstore.jar</code> and Click Add.</li></div><a name="wp81243"> </a><div class="pSmartList2"><li>Click OK.</li></div><a name="wp81244"> </a><div class="pSmartList2"><li>Click Next.</li></div><a name="wp81245"> </a><div class="pSmartList2"><li>Select the Servlet radio button. </li></div><a name="wp81246"> </a><div class="pSmartList2"><li>Click Next.</li></div><a name="wp81247"> </a><div class="pSmartList2"><li>Select <code class="cCode">BannerServlet</code> from the Servlet Class combo box. </li></div><a name="wp81248"> </a><div class="pSmartList2"><li>Click Finish.</li></div></ol></div><a name="wp81252"> </a><div class="pSmartList1"><li>Add the rest of the Web components listed in <a href="Servlets3.html#wp81265">Table 11-2</a>. For each servlet:</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81253"> </a><div class="pSmartList2"><li>Select File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>New<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Web Component.</li></div><a name="wp81254"> </a><div class="pSmartList2"><li>Click the Add to Existing WAR Module radio button. Since the WAR contains all of the servlet classes, you do not have to add any more content.</li></div><a name="wp81255"> </a><div class="pSmartList2"><li>Click Next.</li></div><a name="wp81256"> </a><div class="pSmartList2"><li>Select the Servlet radio button. </li></div><a name="wp81257"> </a><div class="pSmartList2"><li>Click Next.</li></div><a name="wp81258"> </a><div class="pSmartList2"><li>Select the servlet from the Servlet Class combo box. </li></div><a name="wp81318"> </a><div class="pSmartList2"><li>Click Finish.<div align="left"><table border="1" id="wp81265"> <caption><a name="wp81265"> </a><div class="pTableTitle">Table 11-2 Duke's Bookstore Web Components </div></caption> <tr align="center"> <th><a name="wp81271"> </a><div class="pCellHeading">Web Component Name</div></th> <th><a name="wp81273"> </a><div class="pCellHeading">Servlet Class</div></th> <th><a name="wp81275"> </a><div class="pCellHeading">Alias
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -