Fitter-细节-过滤器拦截方式配置
拦截方式配置:资源被访问的方式
注解配置:
设置dispatcherTypes属性
1. REQUEST:默认值。浏览器直接请求资源
2. FORWARD:转发访问资源
3. INCLUDE:包含访问资源
4. ERROR:错误跳转资源
5. ASYNC:异步访问资源
package com.example.day_19_fitterlistener.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * 过滤器快速入门 */ //浏览器直接请求index.jsp资源时,该过滤器会被执行 //@WebFilter(value="/index.jsp",dispatcherTypes = DispatcherType.REQUEST) //只有转发index.jsp时,该过滤器才会被执行 //@WebFilter(value="/index.jsp",dispatcherTypes = DispatcherType.FORWARD) //浏览器直接请求index.jsp资源或者转发index.jsp,该过滤器会被执行 @WebFilter(value="/index.jsp",dispatcherTypes ={ DispatcherType.FORWARD}) public class FilterDemo5 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("FitterDemo5......"); } @Override public void destroy() { Filter.super.destroy(); } }
web.xml配置
设置<dispatcher></dispatcher>标签即可
<filter> <filter-name>demo1</filter-name> <filter-class>com.example.day_19_fitterlistener.filter.FilterDemo1</filter-class> </filter> <filter-mapping> <filter-name>demo1</filter-name> <!-- 拦截路径--> <url-pattern>/*</url-pattern> <!-- <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>ERROR</dispatcher>--> <dispatcher>ASYNC</dispatcher> </filter-mapping>
Fitter-细节-过滤器链(多个过滤器)
过滤器链(配置多个过滤器)
执行顺序:如果有两个过滤器:过滤器1和过滤器2
1. 过滤器1
2. 过滤器2
3. 资源执行
4. 过滤器2
5. 过滤器1
创建FitterDemo6
package com.example.day_19_fitterlistener.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter(value="/*") public class FilterDemo6 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("FitterDemo6......"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("FitterDemo6回来了"); } @Override public void destroy() { Filter.super.destroy(); } }
创建FitterDemo7
package com.example.day_19_fitterlistener.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter(value="/*") public class FilterDemo7 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("FitterDemo7......"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("FitterDemo7回来了"); } @Override public void destroy() { Filter.super.destroy(); } }
运行代码,访问index.jsp
过滤器先后顺序问题:
1. 注解配置:按照类名的字符串比较规则比较,值小的先执行
如: AFilter 和 BFilter,AFilter就先执行了。
2. web.xml配置: <filter-mapping> 谁定义在上边,谁先执行
标签:Fitter,void,Filter,Override,细节,过滤器,import,public From: https://www.cnblogs.com/yuzong/p/17076170.html