zoukankan      html  css  js  c++  java
  • java中的过滤器 --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 Encodingfilter implements Filter {
        //定义属性保存字符编码集
            String encoding;
            
        //构造函数
        public Encodingfilter() {
            System.out.println("过滤器构造函数!");
        }
        //销毁的方法
        public void destroy() {
            System.out.println("过滤器销毁!");
        }
        //过滤的方法
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            System.out.println("过滤器开始过滤!");
            
            //中文乱码处理
            response.setContentType("text/html;charset="+encoding);
            response.setCharacterEncoding(encoding);
            request.setCharacterEncoding(encoding);
            
            //chain对象就是过滤器链对象
            //chain对象的doFilter方法令请求往下一个过滤器传递
            chain.doFilter(request, response);
        }
    
        //初始化的方法
        public void init(FilterConfig fConfig) throws ServletException {
            System.out.println("过滤器初始化!");
            
            //从配置文件中读取字符编码集
            encoding = fConfig.getInitParameter("encoding");
        }
    
    }
    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 Encodingfilter implements Filter {
        //定义属性保存字符编码集
            String encoding;
            
        //构造函数
        public Encodingfilter() {
            System.out.println("过滤器构造函数!");
        }
        //销毁的方法
        public void destroy() {
            System.out.println("过滤器销毁!");
        }
        //过滤的方法
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            System.out.println("过滤器开始过滤!");
            
            //中文乱码处理
            response.setContentType("text/html;charset="+encoding);
            response.setCharacterEncoding(encoding);
            request.setCharacterEncoding(encoding);
            
            //chain对象就是过滤器链对象
            //chain对象的doFilter方法令请求往下一个过滤器传递
            chain.doFilter(request, response);
        }
    
        //初始化的方法
        public void init(FilterConfig fConfig) throws ServletException {
            System.out.println("过滤器初始化!");
            
            //从配置文件中读取字符编码集
            encoding = fConfig.getInitParameter("encoding");
        }
    
    }

    xml配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>ch03</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>

    <context-param> <param-name>userName</param-name> <param-value>sa</param-value> </context-param> <context-param> <param-name>pwd</param-name> <param-value>888</param-value> </context-param> <!--过滤器--> <filter> <display-name>Encodingfilter</display-name> <filter-name>Encodingfilter</filter-name> <filter-class>filter.Encodingfilter</filter-class> <!--初始化--> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Encodingfilter</filter-name> <url-pattern>/*</url-pattern> <!--/*代表所有--> </filter-mapping> </web-app>
  • 相关阅读:
    密码强度正则表达式 – 必须包含大写字母,小写字母和数字,至少8个字符等
    |DataDirectory|的使用
    SqlParameter的作用与用法
    C# 通过http post 请求上传图片和参数
    webuploader批量导入文件
    MVC使用Controller完成登录验证
    网页特效 模板素材
    C#中的Cookie 添加 读取 删除
    JS jquery jquery.wordexport.js 实现导出word
    初始配置JDK
  • 原文地址:https://www.cnblogs.com/nongzihong/p/10024682.html
Copyright © 2011-2022 走看看