java中的过滤器 --Filter

本文深入探讨了Java中用于处理中文乱码问题的字符编码过滤器。通过实现Filter接口,自定义过滤器类,该过滤器可在Web应用中统一设置字符编码,确保数据正确解析,避免乱码出现。文章提供了完整的代码示例和web.xml配置说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

 
 
 
 
 
 
 
posted @ 2018-11-27 09:59 梓鸿 阅读(...) 评论(...) 编辑 收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值