【Head First Servlets and JSP】筆記 28: 過濾器與封裝器

來源:互聯網
上載者:User

標籤:package   code   for   沒有   import   sch   dog   ram   伺服器   

1、過濾器的執行順序: <url-pattern> 為第一梯隊, <servlet-name> 為第二梯隊,梯隊內的執行順序和 DD 裡的聲明順序相同。

When the container recives a request, it first finds all the filter mappings with a <url-pattern> that matches the request URI. This becomes the first set of filters in the filter chain.Next it finds all the filter mappings with a <servlet-name> that matches the request URI. This becomes the second set of filters in the filter chain.In both the sets the filters are executed in the order in which they are declared in the D.D.

 

2、可以對伺服器中傳遞的請求進行過濾。

    <filter>        <filter-name>BeerRequest</filter-name>        <filter-class>sample.BeerRequestFilter</filter-class>        <init-param>            <param-name>LogFileName</param-name>            <param-value>Userlog.txt</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>BeerRequest</filter-name>        <url-pattern>*.do</url-pattern>        <servlet-name>jack</servlet-name>        <dispatcher>REQUEST</dispatcher> <!-- 當一個也沒有設定時,它是預設值 -->        <dispatcher>INCLUDE</dispatcher>        <dispatcher>FORWARD</dispatcher>        <dispatcher>ERROR</dispatcher>    </filter-mapping>

 

3、過濾器應用執行個體

package sample;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;public class Servlet extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        PrintWriter printWriter = resp.getWriter();        printWriter.println("calls doGet(req, resp)");        printWriter.println("測試能否輸出中文");        printWriter.close();    }}

filter :

package sample;import javax.servlet.*;import java.io.IOException;public class Filter implements javax.servlet.Filter{    private int visited;    @Override    public void init(FilterConfig filterConfig) throws ServletException {    }    @Override    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {        servletResponse.setContentType("text/html");        servletResponse.setCharacterEncoding("utf-8");        visited ++;        filterChain.doFilter(servletRequest, servletResponse);        System.out.println("visited = " + visited);    }    @Override    public void destroy() {    }}

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <servlet>        <servlet-name>servlet</servlet-name>        <servlet-class>sample.Servlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>servlet</servlet-name>        <url-pattern>/do</url-pattern>    </servlet-mapping>    <filter>        <filter-name>filter</filter-name>        <filter-class>sample.Filter</filter-class>    </filter>    <filter-mapping>        <filter-name>filter</filter-name>        <servlet-name>servlet</servlet-name>    </filter-mapping></web-app>

 

4、封裝器略。

 

 

 


【Head First Servlets and JSP】筆記 28: 過濾器與封裝器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.