DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> CSS入門知識 >> CSS詳解 >> 過濾器過濾一個包內所有servlet
過濾器過濾一個包內所有servlet
編輯:CSS詳解     

過濾器過濾一個包內所有servlet,先看下文件的目錄結構:
文件目錄結構

我們要做的就是用MyFirstFilter過濾servlet包內的所有servlet即DisplayHeader、HelloServlet、SecondServlet。
主要就是兩個注意事項:
1:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package servlet;   import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;   /**  * Servlet implementation class HelloServlet  */ @WebServlet("/servlet/HelloServlet") public class HelloServlet extends HttpServlet {     private static final long serialVersionUID = 1L;       /**      * @see HttpServlet#HttpServlet()      */     public HelloServlet() {         super();         // TODO Auto-generated constructor stub     }       /**      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)      */     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         // TODO Auto-generated method stub   //      String msg = request.getInputStream().toString(); //      response.getWriter().println("????????");         response.getWriter().println(request.getAttribute("ServletPath"));         response.getWriter().println("公司的分公司"); //      request.setAttribute("name", "????????HelloServlet?????marhong"); //      request.getRequestDispatcher("/SecondServlet").forward(request, response);     }       /**      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)      */     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         // TODO Auto-generated method stub         doGet(request, response);     }   }

這裡一定要注意就是那個注釋部分:
這裡寫圖片描述

它原來的值是:@WebServlet(“/HelloServlet”),一定要改成上圖所示的才可以

2.就是web.xml了,代碼如下所示:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 <!--?xml version="1.0" encoding="UTF-8"?-->  <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">         <filter>     <filter-name>MyFirstFilter</filter-name>     <filter-class>com.util.MyFirstFilter</filter-class>     <init-param>     <param-name>encoding</param-name>     <param-value>GBK</param-value>     </init-param>     <init-param>     <param-name>secondAttribute</param-name>     <param-value>filterConfig的第二個參數</param-value>     </init-param>     </filter>     <filter-mapping>     <filter-name>MyFirstFilter</filter-name>     <url-pattern>/servlet/*</url-pattern>    <url-pattern>/checkIn.jsp</url-pattern>     </filter-mapping>   <servlet>       <!-- 類名 -->      <servlet-name>HelloServlet</servlet-name>      <!-- 所在的包 -->      <servlet-class>servlet.HelloServlet</servlet-class>    </servlet>    <servlet-mapping>      <servlet-name>HelloServlet</servlet-name>      <!-- 訪問的網址 -->      <url-pattern>/SummerTest/servlet/HelloServlet</url-pattern>      </servlet-mapping>          <servlet>       <servlet-name>SecondServlet</servlet-name>     <servlet-class>servlet.SecondServlet</servlet-class>     </servlet>      <servlet-mapping>      <servlet-name>SecondServlet</servlet-name>      <!-- 訪問的網址 -->      <url-pattern>/SummerTest/servlet/SecondServlet</url-pattern>        </servlet-mapping>  <servlet>    <!-- 類名 -->    <servlet-name>DisplayHeader</servlet-name>    <!-- 所在的包 -->    <servlet-class>servlet.DisplayHeader</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>DisplayHeader</servlet-name>    <!-- 訪問的網址 -->    <url-pattern>/SummerTest/servlet/DisplayHeader</url-pattern>  </servlet-mapping> </web-app>

注意這兩個地方應該就可以達到目的了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved