继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

使用过滤器解决(Post/Get)中文乱码问题

做一个善良的人
关注TA
已关注
手记 12
粉丝 14
获赞 205

过滤器的好处:使用了过滤器,无论是Get/Post方式,接收参数时,都不需要再额外的进行转码,过滤器会自动的帮我们办这件事,乱码问题很好的解决了。

方式一:配置文件方式配置过滤器

  1. 创建CharacterEncodingFilter类,实现Filter接口
public class CharacterEncodingFilter implements Filter {

    private String encode = "UTF-8";

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        String encoding = arg0.getInitParameter("encode");
        if (encoding != null) {
            encode = encoding;
        }
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse response = (HttpServletResponse) arg1;

        // post方式设置编码格式:
        request.setCharacterEncoding(encode);
        response.setContentType("text/html;charset=" + encode);

        arg2.doFilter(new CharacterEncodingRequest(request), arg1);
    }

    @Override
    public void destroy() {

    }
}

/**
 * 对Get方式传递的请求参数进行编码
 */
class CharacterEncodingRequest extends HttpServletRequestWrapper {

    private HttpServletRequest request;

    public CharacterEncodingRequest(HttpServletRequest request) {
        super(request);
        this.request = request;
    }

    /**
     * 对参数重新编码
     */
    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);
        if (value == null) {
            return null;
        }
        String method = request.getMethod();
        if ("Get".equalsIgnoreCase(method)) {
            try {
                value = new String(value.getBytes("ISO8859-1"),
                        request.getCharacterEncoding());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return value;
    }

}
  1. 在web.xml中配置过滤器
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.filter.util.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encode</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

方式二:注解的方式配置过滤器。这种方式较为简单。对过滤器的配置,以注解的方式定义。

@WebFilter(initParams = { @WebInitParam(name = "encode", value = "UTF-8") }, urlPatterns = { "/*" })
public class CharacterFilter implements Filter {
    private String encoding = "UTF-8";

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        String encode = arg0.getInitParameter("encode");
        if (encode != null) {
            encoding = encode;
        }
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse response = (HttpServletResponse) arg1;

        request.setCharacterEncoding(encoding);
        response.setContentType("text/html;charset=" + encoding);

        arg2.doFilter(new CharacterEncodingRequest(request), arg1);

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}

/**
 * 对Get方式传递的请求参数进行编码
 */
class CharacterEncodingRequest extends HttpServletRequestWrapper {

    private HttpServletRequest request;

    public CharacterEncodingRequest(HttpServletRequest request) {
        super(request);
        this.request = request;
    }

    /**
     * 对参数重新编码
     */
    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);
        if (value == null) {
            return null;
        }
        String method = request.getMethod();
        if ("Get".equalsIgnoreCase(method)) {
            try {
                value = new String(value.getBytes("ISO8859-1"),
                        request.getCharacterEncoding());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return value;
    }

}

欢迎大家批评指正。

打开App,阅读手记
5人推荐
发表评论
随时随地看视频慕课网APP