串级过滤器:一个过滤器接着另外一个过滤器。
例子:
1.FilterDemo.java文件:
import java.io.IOException;
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 FilterDemo implements Filter {
public FilterDemo(){
System.out.println("调用了默认的构造方法");
}
//用户每次访问被过滤的资源,都会由服务器调用该方法实现过滤
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("FilterDemo执行过滤前");
//对请求进行拦截,代码写在这
chain.doFilter(request, response);//放行
//对响应进行拦截,代码写在这
System.out.println("FilterDemo执行过滤后");
}
public void destroy() {
System.out.println("调用了销毁方法");
}
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("调用初始化方法");
}
}
2.FilterDemo2.java文件:
import java.io.IOException;
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 FilterDemo2 implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("FilterDemo2前");
chain.doFilter(request, response);
System.out.println("FilterDemo2后");
}
public void destroy() {
}
}
3.FilterDemo3.java文件:
import java.io.IOException;
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 FilterDemo3 implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("FilterDemo3前");
chain.doFilter(request, response);
System.out.println("FilterDemo3后");
}
public void destroy() {
}
}
4。web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<filter>
<filter-name>FilterDemo</filter-name>
<filter-class>FilterDemo</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDemo</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>FilterDemo2</filter-name>
<filter-class>FilterDemo2</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDemo2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>FilterDemo3</filter-name>
<filter-class>FilterDemo3</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDemo3</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>FilterDemo2</servlet-name>
<servlet-class>FilterDemo2</servlet-class>
</servlet>
<servlet>
<servlet-name>FilterDemo3</servlet-name>
<servlet-class>FilterDemo3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FilterDemo2</servlet-name>
<url-pattern>/servlet/FilterDemo2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FilterDemo3</servlet-name>
<url-pattern>/servlet/FilterDemo3</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5。运行:http://localhost:8080/Filter_project/index.jsp
6。结果:
调用了默认的构造方法
调用初始化方法
FilterDemo执行过滤前
FilterDemo2前
FilterDemo3前
FilterDemo3后
FilterDemo2后
FilterDemo执行过滤后
调用了销毁方法