package yin.filter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class EncodingFilter implements Filter
{
    String encoding;
    @Override
    public void destroy()
    {
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException
    {
        HttpServletRequest req = (HttpServletRequest) request;
        String method = req.getMethod();
        response.setContentType("text/html; charset=" + encoding);
        if ("post".equals(method.toLowerCase()))
        {
            req.setCharacterEncoding(encoding);
        }
        else
        {
            req = new MyHttpServletRequestWrapper(req);
        }
        // 放行...
        chain.doFilter(req, response);
    }
    @Override
    public void init(FilterConfig config) throws ServletException
    {
        encoding = config.getServletContext().getInitParameter("encoding");
        if (encoding == null)
        {
            encoding = "utf-8";
        }
    }
    class MyHttpServletRequestWrapper extends HttpServletRequestWrapper
    {
        HttpServletRequest request;
        String encoding;
        public MyHttpServletRequestWrapper(HttpServletRequest request)
        {
            super(request);
            this.request = request;
            // 获取全局变量
            encoding = request.getServletContext().getInitParameter("encoding");
        }
        @Override
        public String getParameter(String name)
        {
            String string = request.getParameter(name);
            try
            {
                string = new String(string.getBytes("iso-8859-1"), encoding);
            }
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }
            return string;
        }
        @Override
        public String[] getParameterValues(String name)
        {
            String[] names = request.getParameterValues(name);
            for (int i = 0; i < names.length; i++)
            {
                try
                {
                    names[i] = new String(names[i].getBytes("iso-8859-1"), encoding);
                }
                catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                }
            }
            return names;
        }
    }
}

接下来就是配置web.xml了,其实也是很简单的:


    EncodingFilter
    yin.filter.EncodingFilter
  
  
    EncodingFilter
    /*
  

这样基本上也就完成了,不过在运用时可能还会出现乱码(在下以前就出现过,可能是哪一个页面或者servlet中使用了其他的编码方式吧,不过后来不知道怎样就处理好了。。。)

由于字符编码Filter网上有很多,又大多都是大同小异,所以我这也就不是原创的,但是说转载谁的,在下也不清楚到底谁,所以只能打出转载的旗号了。如有错误之处,欢迎指出哦。。。