zoukankan      html  css  js  c++  java
  • Servlet 过滤器 Filter

    过滤器是一个实现了 javax.servlet.Filter 接口的 Java 类。javax.servlet.Filter 接口定义了三个方法:

    下面是对所有编码过滤器

    package filter;
    
    import java.io.IOException;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public class CharacterEncodingFilter implements Filter {
    
        FilterConfig config;
        public void destroy() {
            // TODO Auto-generated method stub
            
        }
        public void doFilter(ServletRequest arg0, ServletResponse arg1,
                FilterChain arg2) throws IOException, ServletException {
            String CharacterEncoding=config.getInitParameter("CharacterEncoding");
            arg0.setCharacterEncoding(CharacterEncoding);
            arg1.setCharacterEncoding(CharacterEncoding);
            arg1.setContentType("text/html;charset="+CharacterEncoding);
            arg2.doFilter(arg0, arg1);
        }
    
        public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub
            this.config=arg0;
        }
    
    }

    Web.xml配置

      <filter>
          <filter-name>CharacterEncodingFilter</filter-name>
          <filter-class>filter.CharacterEncodingFilter</filter-class>
          <init-param>
              <param-name>CharacterEncoding</param-name>
              <param-value>UTF-8</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>CharacterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

    使用多个过滤器

    Web 应用程序可以根据特定的目的定义若干个不同的过滤器。假设您定义了两个过滤器 AuthenFilter 和 LogFilter。您需要创建一个如下所述的不同的映射,其余的处理与上述所讲解的大致相同:

    <filter>
       <filter-name>LogFilter</filter-name>
       <filter-class>LogFilter</filter-class>
       <init-param>
          <param-name>test-param</param-name>
          <param-value>Initialization Paramter</param-value>
       </init-param>
    </filter>
    
    <filter>
       <filter-name>AuthenFilter</filter-name>
       <filter-class>AuthenFilter</filter-class>
       <init-param>
          <param-name>test-param</param-name>
          <param-value>Initialization Paramter</param-value>
       </init-param>
    </filter>
    
    <filter-mapping>
       <filter-name>LogFilter</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <filter-mapping>
       <filter-name>AuthenFilter</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>

    过滤器的应用顺序

    web.xml 中的 filter-mapping 元素的顺序决定了 Web 容器应用过滤器到 Servlet 的顺序。若要反转过滤器的顺序,您只需要在 web.xml 文件中反转 filter-mapping 元素即可。

    例如,上面的实例将先应用 LogFilter,然后再应用 AuthenFilter,但是下面的实例将颠倒这个顺序:

    <filter-mapping>
       <filter-name>AuthenFilter</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <filter-mapping>
       <filter-name>LogFilter</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
  • 相关阅读:
    清理yum源
    XZ压缩
    Linux命令之dot
    calltree查看工程代码中的函数调用关系
    valgrind 打印程序调用树+进行多线程性能分析
    LINUX 性能 测试 优化工具
    TCP/IP(84) 详解
    perf---LINUX内核研究
    廖雪锋笔记3:类型转换
    廖雪锋笔记2:list,tuble
  • 原文地址:https://www.cnblogs.com/caoyc/p/5563396.html
Copyright © 2011-2022 走看看