JavaWeb-Filter
public class QuickFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { //初始化时调用 } /** * 执行拦截的方法.... * * @param servletRequest :请求对象 * @param servletResponse :响应对象 * @param filterChain :过滤器链对象(是否放行) */ @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("QuickFilter拦截了请求..."); // 放行 filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { //关闭时销毁 } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--快速入门--> <!--创建filter对象--> <filter> <filter-name>QuickFilter</filter-name> <filter-class>com.itheima.a_quick.QuickFilter</filter-class> </filter> <!--拦截请求--> <filter-mapping> <filter-name>QuickFilter</filter-name> <url-pattern>/quick.jsp</url-pattern> </filter-mapping> </web-app>
--使用注解配置
@WebFilter(filterName = "TestFilter", initParams = {@WebInitParam(name = "encode", value = "utf-8")}, dispatcherTypes = {DispatcherType.FORWARD, DispatcherType.REQUEST},value = "/*") public class TestFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { } public void init(FilterConfig config) throws ServletException { } }
// 过滤创建时,执行init方法 public void init(FilterConfig config); // 过滤器拦截时,执行doFilter方法 public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain); // 过滤器销毁时,执行destroy public void destroy();
* 创建 服务器启动时,创建filter对象,执行init方法(只执行一次) * 运行(过滤拦截) 用户访问被拦截的目标资源时,都会执行doFilter方法 * 销毁 服务器关闭或项目卸载时,销毁filter对象,执行destroy方法(只执行一次) * 过滤器一定优于servlet等资源创建....
* 精准匹配 用户访问指定目标资源(/show.jsp)时,过滤器拦截 * 目录匹配 用户访问指定目录下的资源(/user/*)时,过滤器拦截 * 后缀匹配 用户访问指定后缀名的资源(*.html | *.js | *.jsp)时,过滤器拦截 * 匹配所有 用户访问该项目下所有的资源(/*)时,过滤器拦截
// @WebFilter("/show.jsp") // 精准匹配 // @WebFilter("/user/*") // 目录匹配 // @WebFilter("*.html") // 后缀匹配 @WebFilter("/*") // 匹配所有 public class UrlPatternFilter implements Filter { public void init(FilterConfig config) throws ServletException { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws ServletException, IOException { // 向下转型 HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; System.out.println("UrlPatternFilter拦截了请求..."); // 放行 chain.doFilter(request, response); } public void destroy() { } }
<filter-mapping> <filter-name>name</filter-name> <url-pattern>/</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
或
@WebFilter(value = "/*.jsp",dispatcherTypes={DispatcherType.REQUEST,DispatcherType.FORWARD})
FORWARD,//转发的
INCLUDE,//包含在页面的
REQUEST,//请求的
ASYNC,//异步的
ERROR;//出错的
1.在web.xml中,filter执行顺序跟<filter-mapping>的顺序有关,先声明的先执行
2.使用注解配置的话,filter的执行顺序跟名称的字母顺序有关,例如AFilter会比BFilter先执行
3.如果既有在web.xml中声明的Filter,也有通过注解配置的Filter,那么会优先执行web.xml中配置的Filter
*注意:
1.执行顺序跟<filter>
的顺序无关
2.<filter-mapping>
的顺序才决定执行顺序