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

📄 servlets8.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
caw.write(&quot;\n&lt;/body&gt;&lt;/html&gt;&quot;);response.setContentLength(caw.toString().getBytes().length);out.write(caw.toString());</code>out.close();public class CharResponseWrapper extends&nbsp;&nbsp;HttpServletResponseWrapper {&nbsp;&nbsp;private CharArrayWriter output;&nbsp;&nbsp;public String toString() {&nbsp;&nbsp;&nbsp;&nbsp;return output.toString();&nbsp;&nbsp;}&nbsp;&nbsp;public CharResponseWrapper(HttpServletResponse response){&nbsp;&nbsp;&nbsp;&nbsp;super(response);&nbsp;&nbsp;&nbsp;&nbsp;output = new CharArrayWriter();&nbsp;&nbsp;}&nbsp;&nbsp;public PrintWriter getWriter(){&nbsp;&nbsp;&nbsp;&nbsp;return new PrintWriter(output);&nbsp;&nbsp;}}<a name="wp75758"> </a></pre></div><a name="wp64627"> </a><p class="pBody"><a  href="Servlets8.html#wp64633">Figure 11-3</a> shows the entry page for Duke's Bookstore with the hit counter.</p><a name="wp64631"> </a><p class="pBody"></p><div align="left"><img src="images/Fig253.gif" height="384" width="381" alt="Duke's Bookstore" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="64633"> </a><strong><font >Figure 11-3    Duke's Bookstore</font></strong></p><a name="wp64634"> </a><h3 class="pHeading2">Specifying Filter Mappings</h3><a name="wp64635"> </a><p class="pBody">A Web container uses filter mappings to decide how to apply filters to Web resources. A filter mapping matches a filter to a Web component by name or to Web resources by URL pattern. The filters are invoked in the order in which filter mappings appear in the filter mapping list of a WAR. You specify a filter mapping list for a WAR using a <code class="cCode">deploytool</code> or by coding them directly in the Web application deployment descriptor as follows</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp67116"> </a><div class="pSmartList1"><li>Declare the filter. This element creates a name for the filter and declares the filter's implementation class and initialization parameters. </li></div><a name="wp67117"> </a><div class="pSmartList1"><li>Map the filter to a Web resource by name or by URL pattern. </li></div><a name="wp72209"> </a><div class="pSmartList1"><li>Constrain how the filter will be applied to requests by choosing one of the enumerated dispatcher options:</li></div><div class="pSmartList2"><ul class="pSmartList2"><a name="wp72187"> </a><div class="pSmartList2"><li><code class="cCode">REQUEST</code>-Only when the request come directly from the client.</li></div><a name="wp72192"> </a><div class="pSmartList2"><li><code class="cCode">FORWARD</code>-Only when the request has been forwarded to a component (see <a  href="Servlets9.html#wp64709">Transferring Control to Another Web Component</a>).</li></div><a name="wp72193"> </a><div class="pSmartList2"><li><code class="cCode">INCLUDE</code>-Only when the request is being processed by a component that has been included (see <a  href="Servlets9.html#wp64695">Including Other Resources in the Response</a>). </li></div><a name="wp73126"> </a><div class="pSmartList2"><li><code class="cCode">ERROR</code>-Only when the request is being processed with the error page mechanism (see <a  href="Servlets4.html#wp76701">Handling Errors</a>).</li></div><a name="wp72198"> </a><p class="pBodyRelative">You can direct the filter to be applied in any combination of the preceding situations by including multiple <code class="cCode">dispatcher</code> elements. If no elements are specified, the default option is <code class="cCode">REQUEST</code>.</p></ul></div></ul></div><a name="wp73532"> </a><p class="pBody">If you want to log every request to a Web application, you would map the hit counter filter to the URL pattern <code class="cCode">/*</code>. Step <a  href="Servlets3.html#wp81343">13.</a> in <a  href="Servlets3.html#wp63984">The Example Servlets</a> shows how to create and map the filters for the Duke's Bookstore application. <a  href="Servlets8.html#wp71810">Table 11-7</a> summarizes the filter definition and mapping list for the Duke's Bookstore application. The filters are matched by servlet name and each filter chain contains only one filter.</p><div align="left"><table border="1" summary="Duke's Bookstore Filter Definition and Mapping" id="wp71810">  <caption><a name="wp71810"> </a><div class="pTableTitle">Table 11-7   Duke's Bookstore Filter Definition and Mapping List&nbsp;</div></caption>  <tr align="center">    <th><a name="wp71820"> </a><div class="pCellHeading">Filter</div></th>    <th><a name="wp71832"> </a><div class="pCellHeading">Class</div></th>    <th><a name="wp71812"> </a><div class="pCellHeading">Servlet</div></th></tr>  <tr align="left">    <td><a name="wp71822"> </a><div class="pCellBody"><code class="cCode">HitCounterFilter</code></div></td>    <td><a name="wp71834"> </a><div class="pCellBody"><code class="cCode">filters.HitCounterFilter</code></div></td>    <td><a name="wp71814"> </a><div class="pCellBody"><code class="cCode">BookStoreServlet</code></div></td></tr>  <tr align="left">    <td><a name="wp71824"> </a><div class="pCellBody"><code class="cCode">OrderFilter</code></div></td>    <td><a name="wp71836"> </a><div class="pCellBody"><code class="cCode">filters.OrderFilter</code></div></td>    <td><a name="wp71816"> </a><div class="pCellBody"><code class="cCode">ReceiptServlet</code></div></td></tr></table></div><p class="pBody"></p><a name="wp64667"> </a><p class="pBody">You can map a filter to one or more Web resources and you can map more than one filter to a Web resource. This is illustrated in <a  href="Servlets8.html#wp64676">Figure 11-4</a>, where filter F1 is mapped to servlets S1, S2, and S3, filter F2 is mapped to servlet S2, and filter F3 is mapped to servlets S1 and S2. </p><a name="wp64674"> </a><p class="pBody"></p><div align="left"><img src="images/Fig264.gif" height="235" width="359" alt="Filter to Servlet Mapping" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="64676"> </a><strong><font >Figure 11-4    Filter to Servlet Mapping</font></strong></p><a name="wp64679"> </a><p class="pBody">Recall that a filter chain is one of the objects passed to the <code class="cCode">doFilter</code> method of a filter. This chain is formed indirectly via filter mappings. The order of the filters in the chain is the same as the order in which filter mappings appear in the Web application deployment descriptor. </p><a name="wp64681"> </a><p class="pBody">When a filter is mapped to servlet S1, the Web container invokes the <code class="cCode">doFilter</code> method of F1. The <code class="cCode">doFilter</code> method of each filter in S1's filter chain is invoked by the preceding filter in the chain via the <code class="cCode">chain.doFilter</code> method. Since S1's filter chain contains filters F1 and F3, F1's call to <code class="cCode">chain.doFilter</code> invokes the <code class="cCode">doFilter</code> method of filter F3. When F3's <code class="cCode">doFilter</code> method completes, control returns to F1's <code class="cCode">doFilter</code> method.</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="Servlets7.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="Servlets9.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 + -