加个过滤器,解决讨厌的乱码

首先在web.xml文件中添加以下配置
 
  filterEncoding
  com.FilterEncoding
  
   encoding
   GBK
  

 

 
  filterEncoding
  /*
 

 

然后就是创建过滤类了

public class FilterEncoding implements Filter {
 private final String DEFAULT_ENCODING = "ASCII";

 private FilterConfig config = null;

 public void destroy() {
  this.config = null;

 }

 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
   throws IOException, ServletException {
  String targetEncoding = config.getInitParameter("encoding");
  System.out.println("in Encoding:" + config.getInitParameter("encoding"));
  if (null == targetEncoding || "".equals(targetEncoding))
   targetEncoding = DEFAULT_ENCODING;
  request.setCharacterEncoding(targetEncoding);
  chain.doFilter(request, response);
 }

 public void init(FilterConfig config) throws ServletException {
  this.config = config;

 }

}

 

你可能感兴趣的:(加个过滤器,解决讨厌的乱码)