一个简单的编码过滤器

package org.util.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 = "GBK";

 public void destroy() {
  // TODO Auto-generated method stub
  
 }

 public void doFilter(ServletRequest arg0, ServletResponse arg1,
   FilterChain arg2) throws IOException, ServletException {
  // TODO Auto-generated method stub
  arg1.setCharacterEncoding("UTF-8");
  arg1.setContentType("text/html;charset=UTF-8");
  
  //arg1.getWriter().write("执行请求前......");
  arg0.setCharacterEncoding(this.encoding);
  arg2.doFilter(arg0, arg1);
  //arg1.getWriter().write("执行响应后......");
 }

 public void init(FilterConfig config) throws ServletException {
  // TODO Auto-generated method stub
  //System.out.printf("缺省编码:%s,配置编码:%s\n",this.encoding,config.getInitParameter("encoding"));
  if (config.getInitParameter("encoding")!=null)
   this.encoding = config.getInitParameter("encoding");
 }

}

相关文章
相关标签/搜索