基于tomcat8 编写字符编码Filter过滤器无效问题的解决方法

时间:2022-04-23 20:17:48

同事遇到编码问题时想做一个解决全站的字符编码过滤器,过滤器类和配置如下:

过滤器类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<span style="font-size:12px;">package com.chaoxing.newspaper.web.filter;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
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;
import javax.servlet.http.HttpServletResponse;
public class CharacterEncodingFilter implements Filter {
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        System.out.println("过滤器执行");
         final HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
         
        //解决post请求的中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        chain.doFilter((ServletRequest) Proxy.newProxyInstance(this.getClass().getClassLoader(), request.getClass().getInterfaces(), new InvocationHandler(){
            @Override
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
                String methodname = method.getName(); //拿到当前的方法
          if(methodname.equals("getParameter")){
            //执行request.getparameter获取结果
            String value = (String) method.invoke(request, args);
            if(value==null){
              return null;
            }
            if(!request.getMethod().equalsIgnoreCase("get")){ //判断是为get请求
              return value;
            }
            System.out.println(value+"|||||");
            //转换编码返回
            value = new String(value.getBytes("UTF-8"),"UTF-8");
            System.out.println(value+"=======");
            return value;
          }
          //交给request执行请求
          return method.invoke(request, args);
          }
            
        } ), res);
    }
    public void init(FilterConfig fConfig) throws ServletException {
        System.out.println("过滤器初始化");
    }
    @Override
    public void destroy() {
        System.out.println("过滤器完成");
    }
}</span>

过滤器配置:

?
1
2
3
4
5
6
7
8
9
<span style="font-size:12px;"> <!--解决全站乱码的filter  -->
 <filter>
  <filter-name>CharacterEncoding</filter-name>
  <filter-class>com.XXX.web.filter.CharacterEncodingFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>CharacterEncoding</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping></span>

但是在前端form的提交方式是get的情况下,得到的仍然是乱码。

刚开始一直纠结于过滤器是不是写错了,编码集是不是写错了,但是经过测试,并没有错,过滤器正常运行,

最后把value.getByte("iso-8859-1","utf-8") 改为 value.getByte("utf-8","utf-8")后台就能得到值,不再是乱码了。最后百度了下,

说tomcat8默认的编码集是utf-8,tomcat8之前默认的编码集是iso-8859-1,恍然大悟......

总结:tomcat7及其版本之前默认的编码集是iso-8859-1,tomcat8默认的字符集已经是UTF-8,所以不再需要为request.getParameter()的结果进行转码,不需要编码编码过滤器类了

以上这篇基于tomcat8 编写字符编码Filter过滤器无效问题的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/fachunfc/article/details/72846439