河南省建设监理协会官网站,百度seo优化推广软件,网站建设app开发合同,安徽质量工程建设网站一、基本介绍 过滤器 Filter 作为 Java 三大器之一#xff0c;在 Java Web 的使用中有很高的地位。所谓过滤器#xff0c;就是实现了 javax.servlet.Filter 接口的服务器端程序#xff0c;就是对事物进行过滤的。在 Web 中的过滤器#xff0c;当然就是对请求进行过滤#…一、基本介绍 过滤器 Filter 作为 Java 三大器之一在 Java Web 的使用中有很高的地位。所谓过滤器就是实现了 javax.servlet.Filter 接口的服务器端程序就是对事物进行过滤的。在 Web 中的过滤器当然就是对请求进行过滤我们使用过滤器就可以对请求进行拦截然后做相应的处理实现许多特殊功能。如登录控制权限管理过滤敏感词汇等。 使用Filter完整的流程是Filter对用户请求进行预处理接着将请求交给Servlet进行预处理并生成响应最后Filter再对服务器响应进行后处理。
二、过滤器原理 当我们使用过滤器时过滤器会对游览器的请求进行过滤过滤器可以动态的分为 3 个部分1. 放行之前的代码2. 放行3. 放行后的代码这 3 个部分分别会发挥不同作用。
第一部分代码会对游览器请求进行第一次过滤在 HttpServletRequest 到达 Servlet 之前拦截客户的 HttpServletRequest。 第二部分根据需要检查 HttpServletRequest也可以修改 HttpServletRequest 头和数据如果还有过滤器那么就继续交给下一个过滤器。 第三部分在 HttpServletRequest 到达客户端之前拦截 HttpServletResponse对返回的 Web 资源再次进行过滤处理。
我们使用过滤器也就是说不止请求会经过过滤器我们的响应也会经过过滤器。 三、过滤器的作用 * Examples that have been identified for this design are* 1) Authentication Filters, 即用户访问权限过滤* 2) Logging and Auditing Filters, 日志过滤可以记录特殊用户的特殊请求的记录等* 3) Image conversion Filters,图像转换过滤器* 4) Data compression Filters 数据转换* 5) Encryption Filters 安全加密* 6) Tokenizing Filters 词法分析* 7) Filters that trigger resource access events 资源访问事件触发过滤器* 8) XSL/T filters * 9) Mime-type chain Filter 文件类型链过滤器
四、过滤器Filter接口 我们学习过滤器肯定就要先看一下官方给我们提供的过滤器接口。下面我们使用 Idea 来查看 Filter。 Filter 有如下几个方法
void init(FilterConfig filterConfig) 用于完成过滤器的初始化。 doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) 实现过滤功能将该方法对每个请求增加额外的处理。 void destroy() 用于过滤器销毁前完成某些资源的回收。
五、使用过滤器Filter
先自定义 FirstFilter 类实现 Filter 接口
public class FirstFilter implements Filter {Override public void init(FilterConfig filterConfig) throws ServletException {Filter.super.init(filterConfig); System.out.println(--------FirstFilter 初始化完成-------); }Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println(------对 First request 进行过滤 --------);//下面这行代码就是放行 filterChain.doFilter(servletRequest, servletResponse); System.out.println(------对 First response 进行过滤 --------);}Override public void destroy() {Filter.super.destroy(); System.out.println(firstFilter 已销毁); }}再自定义 SecondFilter 类实现 Filter 接口
public class SecondFilter implements Filter {Override public void init(FilterConfig filterConfig) throws ServletException {Filter.super.init(filterConfig); System.out.println(--------SecondFilter 初始化完成-------); }Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println(------对 Second request 进行过滤 --------);//下面这行代码就是放行filterChain.doFilter(servletRequest, servletResponse); System.out.println(------对 Second response 进行过滤 --------);}Override public void destroy() {Filter.super.destroy(); System.out.println(SecondFilter 已销毁); }}再修改 WebConfig 配置类
Configuration
public class WebConfig {Bean public FilterRegistrationBean firstFilterRegistrationBean() {FilterRegistrationBeanFilter filterFilterRegistrationBean new FilterRegistrationBean(); // 对哪些路径进行过滤filterFilterRegistrationBean.addUrlPatterns(/*); filterFilterRegistrationBean.setOrder(1); // 设置优先级 ,数字越小优先级越高FirstFilter filter new FirstFilter(); // 绑定过滤器 filterFilterRegistrationBean.setFilter(filter); return filterFilterRegistrationBean; }Bean public FilterRegistrationBean secondFilterRegistrationBean() {FilterRegistrationBeanFilter filterFilterRegistrationBean new FilterRegistrationBean();filterFilterRegistrationBean.addUrlPatterns(/hello); filterFilterRegistrationBean.setOrder(2); // 设置优先级// 绑定过滤器SecondFilter filter new SecondFilter(); filterFilterRegistrationBean.setFilter(filter); return filterFilterRegistrationBean; }}
启动服务器然后我们在浏览器中随便输入一个 url 地址进行访问
浏览器输出
控制台输出
------对 request 进行过滤 ---------
------对 response 进行过滤 -------- 现在我们就已经可以得出两个结论了过滤器并不会管资源是否存在而只会对配置的拦截路径进行拦截。拦截不仅会对请求进行拦截而且还会对相应进行拦截。 六、多个 Filter 的执行顺序 上面我们配置了 2 个过滤器那么我们怎么知道那个过滤器先执行呢
启动服务器然后我们浏览器输入 http://localhost:8080/hello 来进行访问查看控制台输出
------对 First request 进行过滤 --------
------对 Second request 进行过滤 --------
------对 Second response 进行过滤 --------
------对 First response 进行过滤 -------- 我们可以看见 FirstFilter 先进行过滤然后交给 SecondFilter 然后访问资源然后 SecondFilter 对响应进行过滤然后 FirstFilter 对响应进行过滤。图示如下 七、过滤器Filter生命周期
Filter 有三个阶段分别是初始化拦截和过滤销毁。 初始化阶段web 应用程序启动时web 服务器将创建 Filter 的实例对象并调用其 init 方法完成对象的初始化功能从而为后续的用户请求作好拦截的准备工作filter 对象只会创建一次init 方法也只会执行一次。通过 init 方法的参数可获得代表当前 filter 配置信息的 FilterConfig 对象永远只调用一次拦截和过滤阶段只要请求资源的路径和拦截的路径相同那么过滤器就会对请求进行过滤这个阶段在服务器运行过程中会一直循环不管第几次都在调用doGet()doPost() 方法之前。销毁阶段当服务器Tomcat关闭时Web 容器调用 destroy 方法销毁 Filter。destroy 方法在 Filter 的生命周期中仅执行一次。在 destroy 方法中可以释放过滤器使用的资源(永远只调用一次) 八、FilterConfig 和 FilterChain 说明 FilterConfig 和 FilterChain 这2个对象是由服务器 (Tomcat) 在创建和调用 Filter 对象时所传入的这2个对象十分有用FilterConfig 对象可以读取我们配置的初始参数FilterChain 可以实现多个 Filter 之间的连接。
1、FilterConfig
先看一下源码
public interface FilterConfig {String getFilterName();ServletContext getServletContext();String getInitParameter(String var1);EnumerationString getInitParameterNames();
}
里面的方法就 4 个下面我们分别进行讲解
getFilterName()获取 filter 的名称getServletContext()获取 ServletContextgetInitparamter(String var1)获取配置的初始参数的值getInitParamterNames()获取配置的所有参数名称
2、FilterChain
先看一下源码
public interface FilterChain {void doFilter(ServletRequest var1, ServletResponse var2) throws IOException, ServletException;
} 我们查看源码可以发现 FilterChain 就只有一个方法其实这个方法就是用来对拦截进行放行的如果有多个拦截器那么就会继续调用下一个 Filter 进行拦截。doFilter 方法需要传入个参数一个是 ServletRequest一个是 ServletResponse 参数这个直接传入进行。 Tomcat 在调用过滤器时默认就会传入 Request 和 Response这个参数封装了请求和响应我们直接使用就行。ServletResquest 和 ServletResponse 可以直接强转成 HttpServletRequest 和 HttpServletResponse然后使用相应的方法。 将 ServletRequest 转成 HttpServletRequest Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest httpServletRequest (HttpServletRequest) servletRequest;}
九、参考文档
JavaWeb 过滤器 (Filter) 详解
文章转载自: http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.nqlnd.cn.gov.cn.nqlnd.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.nfyc.cn.gov.cn.nfyc.cn http://www.morning.nfpkx.cn.gov.cn.nfpkx.cn http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.lbxhy.cn.gov.cn.lbxhy.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.tsqrc.cn.gov.cn.tsqrc.cn http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.mzhh.cn.gov.cn.mzhh.cn http://www.morning.rgksz.cn.gov.cn.rgksz.cn http://www.morning.bntfy.cn.gov.cn.bntfy.cn http://www.morning.mnlk.cn.gov.cn.mnlk.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.pzcqz.cn.gov.cn.pzcqz.cn http://www.morning.nlzpj.cn.gov.cn.nlzpj.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.ptmgq.cn.gov.cn.ptmgq.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn http://www.morning.wdskl.cn.gov.cn.wdskl.cn http://www.morning.tsynj.cn.gov.cn.tsynj.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.phjyb.cn.gov.cn.phjyb.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.qblcm.cn.gov.cn.qblcm.cn http://www.morning.fgxnb.cn.gov.cn.fgxnb.cn http://www.morning.pntzg.cn.gov.cn.pntzg.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.zlfxp.cn.gov.cn.zlfxp.cn http://www.morning.xywfz.cn.gov.cn.xywfz.cn http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.qxwrd.cn.gov.cn.qxwrd.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.fbdkb.cn.gov.cn.fbdkb.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.zhengdaotang.cn.gov.cn.zhengdaotang.cn http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.zmpsl.cn.gov.cn.zmpsl.cn http://www.morning.bcdqf.cn.gov.cn.bcdqf.cn http://www.morning.swkzr.cn.gov.cn.swkzr.cn http://www.morning.trsfm.cn.gov.cn.trsfm.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.jyzxt.cn.gov.cn.jyzxt.cn http://www.morning.dnphd.cn.gov.cn.dnphd.cn http://www.morning.sqnxk.cn.gov.cn.sqnxk.cn http://www.morning.wrlqr.cn.gov.cn.wrlqr.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.tndhm.cn.gov.cn.tndhm.cn