字符编码过滤器

package cn.itcast.tool.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;
import javax.servlet.http.HttpServletRequest;

public class EncodingFilter implements Filter{
    public void init(FilterConfig arg0) throws ServletException {}
    public void destroy() {}
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        /*处理post请求的编码问题*/
        request.setCharacterEncoding("utf-8");
        HttpServletRequest req=(HttpServletRequest) request;
        /*
         * 掉包request
         * 1、写一个request的装饰类
         * 2、在放行时使用我们自己的request
         */
        if(req.getMethod().equalsIgnoreCase("get")){
            EncodingRequest er=new EncodingRequest(req);
            chain.doFilter(er, response);
        }else{
            chain.doFilter(req, response);
        }
    }
}
package cn.itcast.tool.filter;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class EncodingRequest extends HttpServletRequestWrapper{

    public EncodingRequest(HttpServletRequest request) {
        super(request);
    }
    
    
     public String[] getParameterValues(String name) {
        String[] paramValues=super.getParameterValues(name);
        if(paramValues==null) return null;
        try {
            for(int i=0;i<paramValues.length;i++){
                String param=paramValues[i];
                if(param==null){
                    continue;
                }else{
                    paramValues[i]=new String(param.getBytes("iso-8859-1"),"utf-8");
                }
            }
            return paramValues;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    
    public String getParameter(String name) {
        try {
            String value=super.getParameter(name);
            if(value==null) return null;
            return new String(value.getBytes("iso-8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
      
    public Map getParameterMap() {
        Map<String,String> map=super.getParameterMap();
        if(map==null) return null;
        Set<String> keys=map.keySet();
        try {
            for(String key:keys){
                String value=map.get(key);
                if(value==null){
                    continue;
                }else{
                    value=new String(value.getBytes("iso-8859-1"),"utf-8");
                    map.put(key, value);
                }
            }
            return map;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
     

}

 

posted @ 2016-10-19 14:20  guodaxia  阅读(121)  评论(0编辑  收藏  举报