Filter被稱為過濾器或者攔截器,基本功能就是對調用servler過程的攔截,在servlet進行響應和處理前後實現一些特殊功能。其實,Filter過濾器就是一個實現了javax.servlet.Filter接口的類,在javax.servlet.Filter接口中定義了3個方法:
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>
測試結果如下:
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)
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>