用过滤器来解决JSP中文乱码问题

转载自:http://www.cnblogs.com/liuling/archive/2012/12/17/asdfsdfa.html

首先写一个过滤器的类,如下:

package com.zrcx.web.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by Administrator on 2016/6/24.
 */
public class CharFilter implements Filter {
    class MyRequest extends HttpServletRequestWrapper {

        @Override
        public String getParameter(String param) {
            String value = null;
            try {
                //post
                super.setCharacterEncoding(encoding);//把编码转换为encoding
                value = super.getParameter(param);
                if (super.getMethod().equalsIgnoreCase("GET")) {
                    if (value != null) {
                        value = new String(value.getBytes("iso8859-1"), encoding);
                    }
                }
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return value;
        }

        public MyRequest(HttpServletRequest request) {
            super(request);
        }

    }

    protected String encoding = null;

    public void destroy() { //销毁
        // TODO Auto-generated method stub
        this.encoding = null;
    }

    //对编码问题进行转换
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=" + encoding);
        //过滤未登录用户
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String path = req.getServletPath();
        String param = req.getQueryString();
        if (path != null) {
            path = path + "?" + param;//全请求路径
        }
        if (path.endsWith("myAddress") || path.endsWith("myJingDong") || path.indexOf("myShouCang") != -1 || path.endsWith("updateUser") || path.indexOf("showOrder") != -1 || path.indexOf("showValidOrder") != -1 || path.indexOf("showCancelOrder") != -1 || path.indexOf("fillOrder") != -1) {
            HttpSession session = req.getSession();
            String userName = (String) session.getAttribute("username");
            if (userName == null) {
                session.setAttribute("url", path.replaceFirst("/", ""));
                System.out.println(session.getAttribute("url"));
                resp.sendRedirect("user.do?op=loginAction");
                return;
            }
        }
        //把过滤器给下一个过滤器或资源处理器
        chain.doFilter(new MyRequest((HttpServletRequest) request), response);
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
        this.encoding = filterConfig.getInitParameter("encoding");//encoding在web.xml中指定
    }

}
然后在web.xml对该过滤器进行注册和映射:

    <filter>
        <filter-name>CharFilter</filter-name>
        <filter-class>com.zrcx.web.filter.CharFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
上面写的过滤器MyFilter类,本来只能处理post提交的数据(post是先处理后接收,get是先接收后处理)。

但是MyFilter里面在对任何页面过滤的时候,来了一个偷梁换柱:把原来客户端请求的request给换掉了,换成自己定义的一个request了,即内部类MyRequest,不过该类要继承一个类HttpServletRequestWrapper。

在自定义的一个内部类MyRequest里面,实现了一个好强大的功能,就是重写了request的getParameter()方法。该方法里面即处理了post提交,又能处理get提交,返回的值就是处理后的值,所以该过滤器就能实现处理post和get提交的乱码问题!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值