Java Web之Filter
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文件如下:
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>
测试结果如下:
2、Filter链
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)
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>