zoukankan      html  css  js  c++  java
  • jsp servlet中的过滤器Filter配置总结(转)

    在Java web开发中常会使用到功能强大的过滤器,他毕竟能给我们带来很大的方便,但是针对过滤的资源我们需要详细的了解他们在web.xml中的配置信息。这个根据几种常用的不同情况进行了总结:

    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>
    </filter-mapping>

    其中<url-pattern>*.html</url-pattern>要过滤jsp那么就改*.html为*.jsp,但是注意没有“/”斜杠

    如果要同时过滤多种类型资源:

    <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>
    </filter-mapping>

    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>
    </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>

    以上都要注意是否有斜杠“/”

  • 相关阅读:
    sprintboot+spring security +jwt 实现登录
    IntelliJ IDEA 上传项目到gitlab
    springboot 引入 spring data jpa
    springboot 引入 restTempalte
    StringRedisTemplate与RedisTemplate 区别
    HTTP协议响应篇
    HTTP协议请求篇
    HTTP协议基础篇(帮助理解)
    TP5验证码的使用
    ThinkPHP5的简单使用
  • 原文地址:https://www.cnblogs.com/likehua/p/1970786.html
Copyright © 2011-2022 走看看