过滤器拦截路径配置
1、具体资源路径:/index.jsp 只有访问index.jsp资源时,过滤器才会被执行
2、拦截目录:/user/* 访问/user下的所有资源时,过滤器都会被执行
3、后缀名拦截:*.jsp 访问所有后缀名为jsp资源时,过滤器都会被执行
4、拦截所有资源:/* 访问所有资源时,过滤器都会被执行
//@WebFilter("/index.jsp")//1、具体资源路径:/index.jsp 只有访问index.jsp资源时,过滤器才会被执行 //@WebFilter("/user/*")//2、拦截目录:/user/* 访问/user下的所有资源时,过滤器都会被执行 //@WebFilter("*.jsp")//3、后缀名拦截:*.jsp 访问所有后缀名为jsp资源时,过滤器都会被执行 @WebFilter("/*")//4、拦截所有资源:/* 访问所有资源时,过滤器都会被执行 public class FilterDemo4 implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { System.out.println("FilterDemo4..."); //放行 chain.doFilter(req, resp); } public void init(FilterConfig config) throws ServletException { } public void destroy() { } }
过滤器拦截方式配置
拦截方式配置:资源被访问的方式
注解配置:
设置dispatcherTypes属性
1、REQUEST:默认值。浏览器直接请求资源
2、FORWARD:转发访问资源
3、INCLUDE:包含访问资源
4、ERROR:错误跳转资源
5、ASYNC:异步访问资源
//浏览器直接请求资源时,该过滤器会被执行 //@WebFilter(value = "/*",dispatcherTypes = DispatcherType.REQUEST) //只有转发访问index.jsp时,该过滤器才会被执行 //@WebFilter(value = "/*",dispatcherTypes = DispatcherType.FORWARD) //浏览器直接请求index.jsp页面或者转发访问index.jsp页面,过滤器会执行 @WebFilter(value = "/index.jsp",dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST}) public class FilterDemo5 implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { System.out.println("FilterDemo5..."); //放行 chain.doFilter(req, resp); } public void init(FilterConfig config) throws ServletException { } public void destroy() { } }
web.xml配置:
设置:<dispatcher></dispatcher>标签
<filter> <filter-name>filedemo1</filter-name> <filter-class>com.tomcat1.web.filter.FilterDemo1</filter-class> </filter> <filter-mapping> <filter-name>filedemo1</filter-name> <url-pattern>/*</url-pattern> <dispatcher>FORWARD</dispatcher> </filter-mapping>
过滤器链(配置多个过滤器)
执行顺序:如果有两个过滤器:过滤器1和过滤器2
1、过滤器1
2、过滤器2
3、资源执行
4、过滤器2
5、过滤器1
@WebFilter("/*") public class FilterDemo6 implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { System.out.println("FilterDemo6执行了..."); //放行 chain.doFilter(req, resp); System.out.println("FilterDemo6返回..."); } public void init(FilterConfig config) throws ServletException { } public void destroy() { } }
@WebFilter("/*") public class FilterDemo7 implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { System.out.println("FilterDemo7执行了..."); //放行 chain.doFilter(req, resp); System.out.println("FilterDemo7返回..."); } public void init(FilterConfig config) throws ServletException { } public void destroy() { } }
过滤器先后顺序:
1、注解配置:按照类名的字符串比较规则比较,值小的先执行
如:AFilter和BFilter,AFilter先执行
2、web.xml配置:谁定义在上面谁先执行
<filter> <filter-name>filedemo1</filter-name> <filter-class>com.tomcat1.web.filter.FilterDemo7</filter-class> </filter> <filter-mapping> <filter-name>filedemo1</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>filedemo2</filter-name> <filter-class>com.tomcat1.web.filter.FilterDemo6</filter-class> </filter> <filter-mapping> <filter-name>filedemo2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
标签:index,void,配置,jsp,过滤器,拦截,public From: https://www.cnblogs.com/xjw12345/p/16598072.html