过滤器

作者: 打死你的小乌龟 | 来源:发表于2018-09-07 18:26 被阅读0次

Filter被称为过滤器或者拦截器其基本功能就是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应后实现一些特殊功能。
Filter 的过滤器就是实现了一个javax.servlet.Filter接口的类,在javax.servlet.Filter接口中定义了三个方法

init(FilterConfig filterConfig)初始化过滤器
doFilter(ServletRequest request,ServletResponse response,FilterChain chain)
destory();

生命周期:
诞生:过滤器的实例是在应用被加载时就完成的实例化,并初始化的。
存活:和应用的生命周期一致的。在内存中是单例的。针对拦截范围内的资源访问,每次访问都会调用void doFIlter(request,response.chain)进行拦截。
死亡:应用被卸载。

拦截器的使用实现Filter,然后web.xml中配置
Filter映射
1.使用“*”通配符拦截所有
2.拦截不同方式的访问请求
web.xml文件中,一个<filter-mapping>元素用于配置一个Filter所负责拦截的资源
<filter-mapping>元素有一个特殊 的子元素<dispatcher>用于指定过滤器所拦截的资源被
Servlet容器调用的方式。

  <filter>
  <filter-name>qq3</filter-name>
  <filter-class>Demo1</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>qq3</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  </filter-mapping>

1.request
直接访问页面会过滤
如果目标资源是通过RequestDispatch的include()或者forword()方法访问时该过滤器不会被调用
2.include
如果目标资源是通过RequestDispatch的include()访问时会调用
3.forword
如果目标资源是通过RequestDispatch的forword()方法访问时会调用
4.error
异常处理机制会处理

Filter链(串联过滤器)

多个Filter程序对同一个URL进行拦截这些Filter就会组成一个Filter链(也叫过滤器链)

Filter链用FilterChain对象来表示,FilterChain中有一个doFilter()方法,作用就是让
Filter链上的当前过滤器放行,请求进入下一个Filter;

一个过滤器接着另外一个过滤器。执行的顺序
  <filter-mapping>的先后顺序决定

过滤器解决乱码

//解决post方式请求参数和响应编码问题的过滤器
    public class SetCharacterEncodingFilter implements Filter {
    private FilterConfig filterConfig;
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        
        String encoding = filterConfig.getInitParameter("encoding");
        if(encoding==null){//忘记配置该参数
            encoding = "UTF-8";//默认编码

    request.setCharacterEncoding(encoding);//只能解决POST请求参数的中文问题
    response.setCharacterEncoding(encoding);//输出流编码
    response.setContentType("text/html;charset="+encoding);//输出流编码,通知了客户端应该使用的编码
    chain.doFilter(request, response);
}
--------------------------------------------------------------------------
 <filter>
  <filter-name>qq3</filter-name>
  <filter-class>Demo1</filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>qq3</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
----------------------------------------------------------------------
public class SetCharacterEncodingFilter implements Filter {
    private FilterConfig filterConfig;
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        
    String encoding = filterConfig.getInitParameter("encoding");    
    request.setCharacterEncoding(encoding);//只能解决POST请求参数的中文问题
    response.setCharacterEncoding(encoding);
    response.setContentType("text/html;charset="+encoding);
    chain.doFilter(request, response);


控制动态资源不缓存

public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        
//      HttpServletRequest request = (HttpServletRequest)req;
//      HttpServletResponse response = (HttpServletResponse)resp;
        
        HttpServletRequest request = null;
        HttpServletResponse response = null;
        try{
            request  = (HttpServletRequest)req;
            response = (HttpServletResponse)resp;
        }catch(Exception e){
            throw new RuntimeException("not-http request or response");
        }
        
        
        response.setHeader("Expires", "-1");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        chain.doFilter(request, response);
    }

控制静态资源缓存时间

    <init-param>
        <param-name>html</param-name>
        <param-value>1</param-value>
    </init-param>
    <init-param>
        <param-name>css</param-name>
        <param-value>2</param-value>
    </init-param>
    <init-param>
        <param-name>js</param-name>
        <param-value>3</param-value>
    </init-param>
--------------------------------------------
public class StaticResourcesNeedCacheFilter implements Filter {
    private FilterConfig filterConfig;
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = null;
        HttpServletResponse response = null;
        try{
            request  = (HttpServletRequest)req;
            response = (HttpServletResponse)resp;
        }catch(Exception e){
            throw new RuntimeException("not-http request or response");
        }
        long time = 0;//缓存的时间
        
        //根据用户请求的uri地址的后缀:
        String uri  = request.getRequestURI();
        String exName = uri.substring(uri.lastIndexOf(".")+1);
        if("html".equals(exName)){
            String value = filterConfig.getInitParameter("html");//小时
            time = Long.parseLong(value)*60*60*1000;
        }
        if("css".equals(exName)){
            String value = filterConfig.getInitParameter("css");//小时
            time = Long.parseLong(value)*60*60*1000;
        }
        if("js".equals(exName)){
            String value = filterConfig.getInitParameter("js");//小时
            time = Long.parseLong(value)*60*60*1000;
        }
        
        response.setDateHeader("Expires", System.currentTimeMillis()+time);
        chain.doFilter(request, response);

相关文章

  • VUE过滤器和计算属性

    过滤器主要分为全局过滤器和局部过滤器。 全局过滤器如下: 局部过滤器如下: 计算属性如下: 过滤器中获取日期: 计...

  • 自定义过滤器的封装

    封装自定义过滤器 引入过滤器 添加+注册过滤器 使用过滤器

  • vue的过滤器及计算属性

    1,过滤器:让要显示在页面上的内容进行重新筛选2,过滤器分为全局过滤器和局部过滤器全局过滤器: 局部过滤器: 3,...

  • 2018-09-18 vue初学六(过滤器:filter(fil

    1.1过滤器 过滤器分为两种:1、全局过滤器:filter2、局部过滤器:filters 1.2过滤器 (当前时间...

  • 过滤器

    ...过滤器分为全局过滤器和局部过滤器全局过滤器的格式:html:{{数据,全局过滤器的名字}}//解析数据js:...

  • 六、过滤器的使用 ------ 2020-05-07

    1、过滤器的作用: 2、全局过滤器 3、局部过滤器 4、过滤器的使用

  • 过滤器和计算属性

    1、过滤器 过滤器指让要显示在页面上的内容进行重新筛选。 过滤器分为两种:全局过滤器、局部过滤器。 2、全局过滤器...

  • Vue的节点

    过滤器Filters 过滤器函数,必须定义在filters节点之下,过滤器本质是函数 分为私有过滤器和全局过滤器 ...

  • Vue进阶(1)

    一.过滤器 过滤器分为局部过滤器和全局过滤器 1.局部过滤器 格式 2.全局过滤器 格式 练习 1.当数字为小于1...

  • 不锈钢锥形过滤器@管道锥形过滤器@冲孔进水口过滤器

    不锈钢锥形过滤器@管道锥形过滤器@冲孔进水口过滤器 产品介绍:临时过滤器又称锥型过滤器,属于管道粗过滤器系列最简单...

网友评论

      本文标题:过滤器

      本文链接:https://www.haomeiwen.com/subject/vsthgftx.html