Java深入 - Filter過濾器

來源:互聯網
上載者:User

標籤:servlet   bootstrap   lang   xxx   配置   mit   div   oca   實現   

Java的1.3開始,對servlet2.3規範中增加了過濾器的支援。

過濾器可以讓我們對目標資源的請求和響應進行截取。

一些filter的特性:

1. Filter是Servlet規範的規定,須要Servlet容器的支援。

2. Filter不能使用Spring架構中的資來源物件。

3. Filter僅僅在Servlet前後起作用。


Filter實現

我們須要實現介面Filter中定義的方法:

/* * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the "License").  You may not use this file except * in compliance with the License. * * You can obtain a copy of the license at * glassfish/bootstrap/legal/CDDLv1.0.txt or * https://glassfish.dev.java.net/public/CDDLv1.0.html. * See the License for the specific language governing * permissions and limitations under the License. * * When distributing Covered Code, include this CDDL * HEADER in each file and include the License file at * glassfish/bootstrap/legal/CDDLv1.0.txt.  If applicable, * add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your * own identifying information: Portions Copyright [yyyy] * [name of copyright owner] * * Copyright 2005 Sun Microsystems, Inc. All rights reserved. * * Portions Copyright Apache Software Foundation. */package javax.servlet;import java.io.IOException;public interface Filter {public void init(FilterConfig filterConfig) throws ServletException;    public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException;public void destroy();}

自己定義一個TestFilter

/** * 自己定義一個Filter * @author zhuli * @date 2014-7-20 */public class TestFilter implements Filter {    @Override    public void init(FilterConfig filterConfig) throws ServletException {        System.out.println("TestFiltern init"); //初始化容器的時候,這邊會執行    }    @Override    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {        // TODO Auto-generated method stub        HttpServletRequest request2 = (HttpServletRequest)request;        String ip = IPUtil.getClientIp(request2);        System.out.println("TestFiltern doFilter 請求前面就處理了 + ip: " + ip);        chain.doFilter(request, response); //傳遞filter鏈        System.out.println("業務處理完了之後。會繼續調用這個filter。然後調用這邊");    }    @Override    public void destroy() {        // TODO Auto-generated method stub        System.out.println("TestFiltern destroy"); //銷毀容器的時候,這邊會執行    }}

結果:

TestFiltern doFilter 請求前面就處理了 + ip: 127.0.0.1
商務邏輯=========
業務處理完了之後,會繼續調用這個filter,然後調用這邊


Filter配置

在web.xml中,配置servlet前面配置Filter就可以:

url-pattern能夠配置符合哪些請求的須要走這個filter

<!-- TEST filter --><filter><filter-name>testFilter</filter-name><filter-class>com.xxx.test.TestFilter</filter-class></filter><filter-mapping><filter-name>testFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>


Filter的運行順序

能夠先看一張圖:

如果我們有兩個過濾器,TestFilter和TestFilter1。

當web請求進來

->

我們運行TestFilter過濾器中chain.doFilter之前的代碼(比如我們的範例中System.out.println("TestFiltern doFilter 請求前面就處理了");)

->

然後運行TestFilter2過濾器中chain.doFilter之前的代碼

->

然後Servlet中的service方法

->

然後處理TestFilter2過濾器中chain.doFilter之後的代碼

->

然後處理TestFilter過濾器中chain.doFilter之後的代碼(比如我們的範例中System.out.println("業務處理完了之後。會繼續調用這個filter,然後調用這邊");)


Filter生命週期

1. init初始化。在web容器啟動的時候,對Filter進行初始化。

初始化會調用init()方法

2. 過濾。詳細過濾是doFilter()方法

3. destroy銷毀。在容器關閉的時候會對Filter進行銷毀。


Java深入 - Filter過濾器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.