程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> Java Web之Filter

Java Web之Filter

編輯:JAVA編程入門知識

Filter被稱為過濾器或者攔截器,基本功能就是對調用servler過程的攔截,在servlet進行響應和處理前後實現一些特殊功能。其實,Filter過濾器就是一個實現了javax.servlet.Filter接口的類,在javax.servlet.Filter接口中定義了3個方法:

  • init(FilterConfig filterConfig) : 用來初始化過濾器,可以在init()中完成與構造方法類似的初始化功能,如果初始化代碼中要使用FilterConfig對象,那麼,這些初始化代碼就只能在Filter的init()方法中編寫,而不能在構造方法中編寫。
  • doFilter(ServletRequest request, ServletResponse response, FilterChain chain) : request和response為Web服務器或Filter鏈中的上一個Filter對象傳遞過來的請求和響應對象;參數chain代表Filter鏈的對象,在當前Filter對象中的doFilter()方法內需要調用FilterChain對象的doFilter()方法,才能把請求交付給Filter鏈的下一個Filter或者目標程序去處理。
  • public void destroy() : destroy()方法在Web服務器卸載Filter之前被調用,該方法用於釋放Filter對象打開的資源,例如關閉數據庫和IO流。

1、實現第一個Filter程序

Index.java文件如下:

 package com.net;
 
 import java.io.IOException;
 import java.io.PrintWriter;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 public class Index extends HttpServlet {
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         PrintWriter out = response.getWriter();
         out.println("Index");
     }
 
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
     }
 }

MyFilter.java文件如下:

 package com.net;
 
 import java.io.IOException;
 import java.io.PrintWriter;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 
 public class MyFilter implements Filter {
     public void init(FilterConfig fConfig) throws ServletException {
         System.out.println("MyFilter.init()");
     }
 
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
         PrintWriter out = response.getWriter();
         out.println("Hello MyFilter");
         chain.doFilter(request, response);
     }
     
     public void destroy() {
         System.out.println("MyFilter.destroy()");
     }
 }

web.xml配置文件添加如下代碼:

 <filter>
     <filter-name>MyFilter</filter-name>
     <filter-class>com.net.MyFilter</filter-class>
 </filter>
 <filter-mapping>
     <filter-name>MyFilter</filter-name>
     <url-pattern>/Index</url-pattern>
 </filter-mapping>
 
 
 <servlet>
     <servlet-name>Index</servlet-name>
     <servlet-class>com.net.Index</servlet-class>
 </servlet>
 <servlet-mapping>
     <servlet-name>Index</servlet-name>
     <url-pattern>/Index</url-pattern>
 </servlet-mapping>

測試結果如下:

 

2、Filter鏈

Filter鏈示例圖如下:

測試程序如下,Index.java文件和MyFilter.java文件參考實現第一個Filter程序中代碼,MyFilter2.java文件和web.xml配置文件如下:

MyFilter2.java文件如下:

 package com.net;
 
 import java.io.IOException;
 import java.io.PrintWriter;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 
 public class MyFilter2 implements Filter {
     public void init(FilterConfig fConfig) throws ServletException {
     }
 
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
         PrintWriter out = response.getWriter();
         out.println("Hello MyFilter2 Start");
         chain.doFilter(request, response);
         out.println("Hello MyFilter2 End");
     }
     
     public void destroy() {
     }
 }

web.xml配置文件添加如下代碼:

<filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>com.net.MyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/Index</url-pattern>
</filter-mapping>

<filter>
    <filter-name>MyFilter2</filter-name>
    <filter-class>com.net.MyFilter2</filter-class>
</filter>
<filter-mapping>
    <filter-name>MyFilter2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>Index</servlet-name>
    <servlet-class>com.net.Index</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Index</servlet-name>
    <url-pattern>/Index</url-pattern>
</servlet-mapping>

測試結果如下:

注意:Filter之間的調用順序什麼樣的呢?這與他們在web.xml中聲明的前後順序有關, 比如按照上面web.xml配置,Filter的調用順序為:

請求->MyFilter -> MyFilter2 -> Index(servlet)

響應 <- MyFilter <- MyFilter2 <- Index(servlet)

 

3、Filter的生命周期

Filter的創建和銷毀由web服務器負責。 web 應用程序啟動時,web 服務器將創建Filter 的實例對象,並調用其init方法,完成對象的初始化功能,從而為後續的用戶請求作好攔截的准備工作,filter對象只會創建一次,init方法也只會執行一次。通過init方法的參數,可獲得代表當前filter配置信息的FilterConfig對象。FilterConfig接口封裝了Filter程序在web.xml中所有注冊信息,並提供了一些獲取這些配置信息的方法,如下所示:

MyFilter.java文件如下

package com.net;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyFilter implements Filter {
    public void init(FilterConfig fConfig) throws ServletException {
        System.out.println("MyFilter.init()");
        
        //得到過濾器的名字
        String filterName = fConfig.getFilterName();
        //得到在web.xml文件中配置的初始化參數
        String initParam = fConfig.getInitParameter("like");
        
        System.out.println(filterName);
        System.out.println(initParam);
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        out.println("Hello MyFilter");
        chain.doFilter(request, response);
    }
    
    public void destroy() {
        System.out.println("MyFilter.destroy()");
    }
}

MyFilter.java對應的web.xml配置項如下:

<filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>com.net.MyFilter</filter-class>
    <init-param>
        <param-name>like</param-name>
        <param-value>java</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/Index</url-pattern>
</filter-mapping>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved