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文件如下:

 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>

测试结果如下:

Java Web之Filter

2、Filter链

Filter链示例图如下:

Java Web之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>

测试结果如下:

Java Web之Filter

注意: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中所有注册信息,并提供了一些获取这些配置信息的方法,如下所示:

Java Web之Filter

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>
上一篇:JAVA基础-栈与堆,static、final修饰符、内部类和Java内存分配


下一篇:Linux内核线程之深入浅出【转】