如图所示,对于一个指定了过滤器的Web资源(Servlet、JSP、HTML)来说,过滤器拦在客户端与Web资源之间,进出都要经过过滤器。
当请求到来时,先经过过滤器,过滤器可以检查、修改请求的内容再发给对应的Web资源,甚至可以根据请求的内容禁止客户端访问对应的Web资源。
Web资源产生的应答也要先经过过滤器,过滤器可以检查、修改应答的内容再发给客户端。
Filter接口
过滤器必须实现javax.servlet.Filter接口,这个接口包含3个方法:init、doFilter、destroy。
init(FilterConfig filterConfig) :Java EE容器创建过滤器实例后调用这个方法,用于为过滤器做准备工作,可以从filterConfig对象读取配置文件web.xml中为该过滤器设置的初始化参数。
doFilter(ServletRequest request, ServletResponse response, FilterChain chain) :过滤操作在这个方法里实现,包括检查、修改请求对象,检查修改应答对象。参数request是请求对象,response是应答对象,chain用于访问后续过滤器。
destroy() :Java EE容器在销毁过滤器实例前调用这个方法,用于释放资源。
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>myweb</display-name> <filter> <filter-name>filter1</filter-name> <filter-class>hb.filter.Filter1</filter-class> </filter> <filter-mapping> <filter-name>filter1</filter-name> <url-pattern>/filter/*</url-pattern> </filter-mapping> <filter> <filter-name>filter2</filter-name> <filter-class>hb.filter.Filter2</filter-class> </filter> <filter-mapping> <filter-name>filter2</filter-name> <url-pattern>/filter/*</url-pattern> </filter-mapping> </web-app>
package hb.filter; 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 Filter1 implements Filter { public void destroy() { System.out.println("Filter1 destory"); } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { System.out.println("Filter1 doFilter"); chain.doFilter(arg0, arg1); } public void init(FilterConfig arg0) throws ServletException { System.out.println("Filter1 init"); } }
package hb.filter; 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 Filter2 implements Filter{ public void destroy() { System.out.println("Filter2 destory"); } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println("Filter2 doFilter"); } public void init(FilterConfig arg0) throws ServletException { System.out.println("Filter2 init"); } }
<filter> <filter-name>myFilter</filter-name> <filter-class>xx.MyFilter</filte-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <servlet-name>目标资源一</servlet-name> <dispatcher>REQUEST</dispatcher> </filter-mapping> <filter-mapping> <filter-name>myFilter</filter-name> <servlet-name>目标资源二</servlet-name> <dispatcher>REQUEST</dispatcher> </filter-mapping>
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>myweb</display-name> <servlet> <display-name>MyServlet</display-name> <servlet-name>MyServlet</servlet-name> <servlet-class>hb.filter.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet.do</url-pattern> </servlet-mapping> <filter> <filter-name>filter3</filter-name> <filter-class>hb.filter.Filter3</filter-class> </filter> <filter-mapping> <filter-name>filter3</filter-name> <servlet-name>MyServlet</servlet-name> </filter-mapping> </web-app>
package hb.filter; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; public MyServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("servlet"); response.sendRedirect("index.jsp"); } }
package hb.filter; 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 Filter3 implements Filter { public void destroy() { System.out.println("Filter3 destory"); } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { System.out.println("Filter3 doFilter"); chain.doFilter(arg0, arg1);//不能少,否则无法到servlet处理 } public void init(FilterConfig arg0) throws ServletException { System.out.println("Filter3 init"); } }