Filter被稱為過濾器或者攔截器,基本功能就是對調用servler過程的攔截,在servlet進行響應和處理前後實現一些特殊功能。其實,Filter過濾器就是一個實現了javax.servlet.Filter接口的類,在javax.servlet.Filter接口中定義了3個方法:
Index.java文件如下:
1 package com.net; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class Index extends HttpServlet { 12 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 13 PrintWriter out = response.getWriter(); 14 out.println("Index"); 15 } 16 17 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 18 doGet(request, response); 19 } 20 }
MyFilter.java文件如下:
1 package com.net; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 13 public class MyFilter implements Filter { 14 public void init(FilterConfig fConfig) throws ServletException { 15 System.out.println("MyFilter.init()"); 16 } 17 18 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 19 PrintWriter out = response.getWriter(); 20 out.println("Hello MyFilter"); 21 chain.doFilter(request, response); 22 } 23 24 public void destroy() { 25 System.out.println("MyFilter.destroy()"); 26 } 27 }
web.xml配置文件添加如下代碼:
1 <filter> 2 <filter-name>MyFilter</filter-name> 3 <filter-class>com.net.MyFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>MyFilter</filter-name> 7 <url-pattern>/Index</url-pattern> 8 </filter-mapping> 9 10 11 <servlet> 12 <servlet-name>Index</servlet-name> 13 <servlet-class>com.net.Index</servlet-class> 14 </servlet> 15 <servlet-mapping> 16 <servlet-name>Index</servlet-name> 17 <url-pattern>/Index</url-pattern> 18 </servlet-mapping>
測試結果如下:
Filter鏈示例圖如下:
測試程序如下,Index.java文件和MyFilter.java文件參考實現第一個Filter程序中代碼,MyFilter2.java文件和web.xml配置文件如下:
MyFilter2.java文件如下:
1 package com.net; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 13 public class MyFilter2 implements Filter { 14 public void init(FilterConfig fConfig) throws ServletException { 15 } 16 17 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 18 PrintWriter out = response.getWriter(); 19 out.println("Hello MyFilter2 Start"); 20 chain.doFilter(request, response); 21 out.println("Hello MyFilter2 End"); 22 } 23 24 public void destroy() { 25 } 26 }
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)
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>