全局过滤器解决乱码

1.新建一个Filter,并在web.xml中注册

//注意,filter注册需要放在servlet注册的前面,否则报错

        globladecoder
        com.cskaoyan.bookStore.web.filter.GlobalDecodeFilter
   
   
          globladecoder
          /servlet/*
   

2.新建一个过滤器。并实现其dofilter方法

public class GlobalDecodeFilter implements Filter
{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException
    {

        HttpServletRequest req = (HttpServletRequest) request;
        response.setContentType("text/html;charset=utf-8");

        //判断post和get,切记POST一定要大写
        if (req.getMethod().equals("POST"))
        { // Post这么解决
            request.setCharacterEncoding("utf-8");
            chain.doFilter(request, response);
        }
        else
        {
//包装get提交的请求数据
            HttpServletRequest request2 = new MyRequest(
                    (HttpServletRequest) request);
            chain.doFilter(request2, response);
        }

    }

    @Override
    public void destroy()
    {

    }

}

}

3.包装类实现getParameter三个API

class MyRequest extends HttpServletRequestWrapper
{

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

    @Override
    public String getParameter(String name)
    {
//需要传进来最初的request
        String param = super.getParameter(name);
        String key = null;
        try
        {
            byte[] bytes;
            if (param != null)
            {
//解码
                bytes = param.getBytes("iso-8859-1");
                key = new String(bytes, "utf-8");
            }
        }
        catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return key;
    }

@Override
    public Map getParameterMap()
    {
        //Map
        Map  permeterMap = super.getParameterMap();
        //取出map中的映射关系
          Set> entrySet  = permeterMap.entrySet();
        
          //遍历entrySet 取出键值对
          for (Entry entry : entrySet)
        {
            String[] values = entry.getValue();
           for (String string : values)
            {
                try
                {
                    //逐个遍历key的valu值,立即解码
            byte[] bytes = string.getBytes("ISO-8859-1");
                    string = new String(bytes, "utf-8");
                }
                catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return permeterMap;
    }

    @Override
    public String[] getParameterValues(String name)
    {
        //数组
        //拿到包含参数的key和value的map
        Map permeterMap = getParameterMap();
        //通过传进来的key拿到参数值数组(checkbox)
        String[] strings = permeterMap.get(name);   
        return strings;
    }

以后无论是get或者post方式的getParameter的时候都不需要再编码解码了

你可能感兴趣的:(全局过滤器解决乱码)