zoukankan      html  css  js  c++  java
  • JSP过滤器Filter配置过滤类型汇总

    一、配置方法
    1 映射过滤应用程序中所有资源
    <filter>
        <filter-name>loggerfilter</filter-name>
        <filter-class>myfilter.LoggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loggerfilter</filter-name>
        <url-pattern>/*</url-pattern> //访问当前主机,当前应用程序根下的所有文件包括多级子目录下的所有文件,注意这里*前有“/”
    </filter-mapping>

    2 过滤指定的类型文件资源
    <filter>
        <filter-name>loggerfilter</filter-name>
        <filter-class>myfilter.LoggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loggerfilter</filter-name>
       <url-pattern>*.html</url-pattern> //访问当前主机,当前应用程序根目录下的所有html文件,注意:*.html前没有“/”,否则错误
    </filter-mapping>
    其中*.html要过滤jsp那么就改*.html为*.jsp,但是注意没有“/”斜杠。如果要同时过滤多种类型资源:
    方法1 url-pattern 分开写
    <filter>
        <filter-name>loggerfilter</filter-name>
        <filter-class>myfilter.LoggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loggerfilter</filter-name>
        <url-pattern>*.html</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>loggerfilter</filter-name>
        <url-pattern>*.jsp</url-pattern>//访问当前主机,当前应用程序根目录以所有及子目录下的所有jsp文件
    </filter-mapping>

    方法2 将url-pattern合并
    <filter>
        <filter-name>loggerfilter</filter-name>
        <filter-class>myfilter.LoggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loggerfilter</filter-name>
        <url-pattern>*.html;*.jsp</url-pattern>
    </filter-mapping>//*.html;*.jsp两类型之间用分号;间隔

    3 过滤指定的目录下的所有文件
    <filter>
        <filter-name>loggerfilter</filter-name>
        <filter-class>myfilter.LoggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loggerfilter</filter-name>
        <url-pattern>/folder_name/*</url-pattern>//访问当前主机,当前应用程序根目录下的folder_name子目录(可是多级子目录)下所有文件
    </filter-mapping>

    4 过滤指定的servlet
    <filter>
        <filter-name>loggerfilter</filter-name>
        <filter-class>myfilter.LoggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loggerfilter</filter-name>
        <servlet-name>loggerservlet</servlet-name>
    </filter-mapping>
    <servlet>
        <servlet-name>loggerservlet</servlet-name>
        <servlet-class>myfilter.LoggerServlet</servlet-class>
    </servlet>


    5 过滤指定文件(即单一文件)
    <filter>
        <filter-name>loggerfilter</filter-name>
        <filter-class>myfilter.LoggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loggerfilter</filter-name>
        <url-pattern>/simplefilter.html</url-pattern>
    </filter-mapping>

    6  过滤指定目录下的指定类型的所有文件
    这种情况下在web.xml中无法一次性配置完成,需要结合filter的实现类
    首先在web.xml中配置过滤指定目录下的所有文件
    <filter>
        <filter-name>loggerfilter</filter-name>
        <filter-class>myfilter.LoggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loggerfilter</filter-name>
        <url-pattern>/dir_name/*</url-pattern>
    </filter-mapping>
    然后在filter的实现类中的doFilter方法中获取请求的uri或者url,判断uri或者url中是否包含指定文件类型的字符串,决定是否过滤
    //获得用户请求的uri
    String uri = request.getRequestURI();
    if(uri.contains(".jsp")||uri.contains(".html"))
    System.out.println("开始过滤"+url);

    7  过滤指定目录下指定类型的单一文件
    <filter>
        <filter-name>loggerfilter</filter-name>
        <filter-class>myfilter.LoggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loggerfilter</filter-name>
        <url-pattern>/dir_name/index.jsp</url-pattern>
    </filter-mapping>
    以上都要注意是否有斜杠“/”



    二、实例
    不缓存文件
    <filter>
        <filter-name>NoCache</filter-name>
        <filter-class>com.sys.common.Filters.ResponseHeaderFilter
            </filter-class>
        <init-param>
          <param-name>Cache-Control</param-name>
          <param-value>no-cache, must-revalidate</param-value>
        </init-param>
      </filter>

    缓存文件一周
      <filter>
        <filter-name>CacheForWeek</filter-name>
        <filter-class>com.sys.common.Filters.ResponseHeaderFilter
            </filter-class>
        <init-param>
          <param-name>Cache-Control</param-name>
          <param-value>max-age=604800, public</param-value>
        </init-param>
      </filter>
    <filter-mapping>
        <filter-name>CacheForWeek</filter-name>
        <url-pattern>/images/*</url-pattern>
      </filter-mapping>
      <filter-mapping>
        <filter-name>CacheForWeek</filter-name>
        <url-pattern>*.js</url-pattern>
      </filter-mapping>
      <filter-mapping>
        <filter-name>CacheForWeek</filter-name>
        <url-pattern>*.css</url-pattern>
      </filter-mapping>

    自定义的filter:
    public class ResponseHeaderFilter implements Filter {
        FilterConfig fc;
        public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            for (Enumeration e = fc.getInitParameterNames(); e.hasMoreElements();) {
                String headerName = (String) e.nextElement();
                response.addHeader(headerName, fc.getInitParameter(headerName));
            }
            chain.doFilter(req, response);
        }

        public void init(FilterConfig filterConfig) {
            this.fc = filterConfig;
        }

        public void destroy() {
            this.fc = null;
        }

    }

    原帖地址:http://blog.sina.com.cn/s/blog_7ffb8dd501013hqc.html

  • 相关阅读:
    SD卡image 的文件系统分区太小无法安装更多库(如何扩大SD卡rootfs分区)
    ubuntu下生成dtb文件提示:sopc2dts: command not found
    HPS端用于信息打印窗口的 串口的驱动是什么时候加载的呢?
    给 HPS 增添了一个用FPGA逻辑自定义的外设以后, SD卡 image里面哪些文件要更新?
    为何FPGA 外设 IP 与 HPS IP 之间有个 Avalon-MM Pipeline Bridge IP?
    什么是根文件系统(root filesystem)(未完待续)
    为什么preloader和uboot要放置在RAW A2分区?
    重装系统的时候最好选择?(未完待续)
    Android相关知识点面试
    java知识查漏补缺
  • 原文地址:https://www.cnblogs.com/wmcoder/p/5689032.html
Copyright © 2011-2022 走看看