使用Filter和包装模式解决request/response乱码问题

package filter;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
 
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;
import javax.servlet.http.HttpServletResponse;
/**
 * 使用此过滤器解决reqeust/response乱码问题
 * @author 马隆博
 */
public class CharacterFilter implements Filter {
    @Override
    public void init(FilterConfig fConfig) throws ServletException {}
 
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
 
        req.setCharacterEncoding("utf-8");// 解决request乱码
        resp.setContentType("text/html;charset=utf-8"); // 解决response乱码
 
        if ("get".equalsIgnoreCase(request.getMethod())) {
            CharacterRequest characterRequest = new CharacterRequest(request); // 使用包装的request
            chain.doFilter(characterRequest, response);// 放行
            return;
        }
        doFilter(request, response, chain);
    }
 
    @Override
    public void destroy() {}
}
 
/**
 * 包装后的Request
 * @author 马隆博
 */
class CharacterRequest extends HttpServletRequestWrapper {
    public CharacterRequest(HttpServletRequest request) {
        super(request);
    }
 
    @SuppressWarnings("finally")
    private String convertCharacter(String value) {
        String resultValue = value;
        try {
            resultValue = new String(value.getBytes("ISO-8859-1"), "utf-8");//转换为utf-8
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            return resultValue;
        }
    }
 
    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);// 调用被包装对象的方法
        if (value != null){
            value = convertCharacter(value);//转换字符
        }
        return value;
    }
 
    @Override
    public Map<String, String[]> getParameterMap() {
        Map<String, String[]> map = super.getParameterMap();
        if (map != null) {
            // 遍历转换字符
            for (String[] values : map.values()) {
                for (String value : values) {
                    value = convertCharacter(value);
                }
            }
        }
        return map;
    }
 
    @Override
    public String[] getParameterValues(String name) {
        String[] values = super.getParameterValues(name);
        if (values != null) {
            // 遍历转换字符
            for (String value : values) {
                value = convertCharacter(value);
            }
        }
        return values;
    }
}
阅读更多
文章标签: 乱码 filter
个人分类: java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭