美文网首页
全局过滤器解决乱码

全局过滤器解决乱码

作者: 扒块腹肌 | 来源:发表于2017-03-08 15:41 被阅读24次

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

    //注意,filter注册需要放在servlet注册的前面,否则报错
    <filter>
            <filter-name>globladecoder</filter-name>
            <filter-class>com.cskaoyan.bookStore.web.filter.GlobalDecodeFilter</filter-class>
       </filter>
       <filter-mapping>
              <filter-name>globladecoder</filter-name>
              <url-pattern>/servlet/*</url-pattern>
       </filter-mapping>
    

    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<String, String[]> getParameterMap()
        {
            //Map
            Map<String , String[]>  permeterMap = super.getParameterMap();
            //取出map中的映射关系
              Set<Entry<String, String[]>> entrySet  = permeterMap.entrySet();
            
              //遍历entrySet 取出键值对
              for (Entry<String, String[]> 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<String, String[]> permeterMap = getParameterMap();
            //通过传进来的key拿到参数值数组(checkbox)
            String[] strings = permeterMap.get(name);   
            return strings;
        }
    
    

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

    相关文章

      网友评论

          本文标题:全局过滤器解决乱码

          本文链接:https://www.haomeiwen.com/subject/hyqpgttx.html