编写filter,处理get和post中文乱码问题

写web程序遇到中文乱码是无法避免的问题,虽然有好多框架都都做了处理,但是还是对于刚刚学习jsp和servlet的新手来说,乱码还是不好处理,所以几天贴出福利代码包装一下Httprequest处理get乱码问题,post乱码只需要修改字符编码就好了

所以只需要包装处理一下get请求就好了代码如下

package com.leige.filter;



import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * 对GET请求参数加以处理!
 * @author qdmmy6
 *
 */
public class GetRequest extends HttpServletRequestWrapper {
    private HttpServletRequest request;
    private String charset;

    public GetRequest(HttpServletRequest request, String charset) {
        super(request);
        this.request = request;
        this.charset = charset;
    }

    @Override
    public String getParameter(String name) {
        // 获取参数
        String value = request.getParameter(name);
        if(value == null) return null;//如果为null,直接返回null
        try {
            // 对参数进行编码处理后返回
            return new String(value.getBytes("ISO-8859-1"), charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public Map getParameterMap() {
        Map map = request.getParameterMap();
        if(map == null) return map;
        // 遍历map,对每个值进行编码处理
        for(String key : map.keySet()) {
            String[] values = map.get(key);
            for(int i = 0; i < values.length; i++) {
                try {
                    values[i] = new String(values[i].getBytes("ISO-8859-1"), charset);
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        // 处理后返回
        return map;
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] values = super.getParameterValues(name);
        for(int i = 0; i < values.length; i++) {
            try {
                values[i] = new String(values[i].getBytes("ISO-8859-1"), charset);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
        return values;
    }
}

编写filter

package com.leige.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 {
    private String charset = "UTF-8";
    @Override
    public void destroy() {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        if(req.getMethod().equalsIgnoreCase("GET")) {
            if(!(req instanceof GetRequest)) {
                req = new GetRequest(req, charset);//处理get请求编码
            }
        } else {
            req.setCharacterEncoding(charset);//处理post请求编码
        }
        chain.doFilter(req, response);
    }

    @Override
    public void init(FilterConfig fConfig) throws ServletException {
        String charset = fConfig.getInitParameter("charset");
        if(charset != null && !charset.isEmpty()) {
            this.charset = charset;
        }
    }
}

最后在web.xml中配置filter就可以处理中文乱码了,服务器使用什么编码就在代码中修改即可

你可能感兴趣的:(java)