<filter>
  <filter-name>Encoding</filter-name>
  <filter-class>com.aoji.filter.MainFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>GBK</param-value>
  </init-param>
 </filter>

<filter-mapping>
  <filter-name>Encoding</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

public class MainFilter implements Filter {
 protected String encoding = null; // 要制定的编码,在web.xml中配置

 protected FilterConfig filterConfig = null;

 public void destroy() {
  this.encoding = null;
  this.filterConfig = null;
 }

 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  if (request.getCharacterEncoding() == null) {
   String encoding = getEncoding(); // 得到指定的编码名字
   if (encoding != null)
    request.setCharacterEncoding(encoding); // 设置request的编码
  }else{
   request.setCharacterEncoding("GBK");
  }
  //清除缓存
//  ((HttpServletResponse) response).setHeader("Cache-Control", "no-cache");
//  ((HttpServletResponse) response).setHeader("Expires", "0");
//  ((HttpServletResponse) response).setHeader("Pragram", "no-cache");
  chain.doFilter(request, response); // 执行下一个filter
 }

 public void init(FilterConfig filterConfig) throws ServletException {

  this.filterConfig = filterConfig;
  this.encoding = filterConfig.getInitParameter("encoding"); // 得到在web.xml中配置的编码
 }

 protected String getEncoding() {
  return (this.encoding); // 得到指定的编码
 }
}