web項目中的字符編碼Filter

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了,其實也是很簡單的:html

<filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>yin.filter.EncodingFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

這樣基本上也就完成了,不過在運用時可能還會出現亂碼(在下之前就出現過,多是哪個頁面或者servlet中使用了其餘的編碼方式吧,不事後來不知道怎樣就處理好了。。。)java

因爲字符編碼Filter網上有不少,又大多都是大同小異,因此我這也就不是原創的,可是說轉載誰的,在下也不清楚到底誰,因此只能打出轉載的旗號了。若有錯誤之處,歡迎指出哦。。。
web

相關文章
相關標籤/搜索