步骤一: package com.aptech.jb.epet.util; 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; /** * 过滤器文件继承Filter * */ public class GBFilter implements Filter{ //动态指定格式 String myfilter = "gbk"; //销毁的处理方式 public void destroy() { System.out.println("destory"); } //提交表单时设置拦截 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(myfilter); response.setCharacterEncoding(myfilter); chain.doFilter(request, response); } //初始化时获取配置文件信息 public void init(FilterConfig config) throws ServletException { if(config.getInitParameter("gb") != null && config.getInitParameter("gb") == ""){ myfilter = config.getInitParameter("gb"); } } } 步骤二 : <!-- 中文过滤器 --> <filter> <filter-name>GBFilter</filter-name> <filter-class>com.aptech.jb.epet.util.GBFilter</filter-class> <init-param> <param-name>gb</param-name> <param-value>gbk</param-value> </init-param> </filter> <filter-mapping> <filter-name>GBFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>