Loading

Filter - 全局编码 (装饰者模式)

Tomcat7以前需要处理get乱码,tomcat8以后不需要处理get乱码,因为tomcat内部会处理。 

乱码原因:

  1 package cn.itcast.web.filter;
  2 
  3 import java.io.IOException;
  4 import java.io.UnsupportedEncodingException;
  5 import java.util.Map;
  6 import javax.servlet.Filter;
  7 import javax.servlet.FilterChain;
  8 import javax.servlet.FilterConfig;
  9 import javax.servlet.ServletException;
 10 import javax.servlet.ServletRequest;
 11 import javax.servlet.ServletResponse;
 12 import javax.servlet.annotation.WebFilter;
 13 import javax.servlet.http.HttpServletRequest;
 14 import javax.servlet.http.HttpServletRequestWrapper;
 15 
 16 /**
 17  * 编码过滤器(用于统一项目编码,防止出现乱码)
 18  */
 19 @WebFilter("/*")
 20 public class EncodingFilter2 implements Filter {
 21     public void init(FilterConfig filterConfig) throws ServletException {
 22 
 23     }
 24 
 25     public void doFilter(ServletRequest request, ServletResponse response,
 26                          FilterChain chain) throws IOException, ServletException {
 27         // 处理请求乱码
 28         HttpServletRequest httpServletRequest = (HttpServletRequest) request;
 29         HttpServletRequest myRequest = new MyRequest(httpServletRequest);
 30         // 处理响应乱码
 31         response.setContentType("text/html;charset=utf-8");
 32         chain.doFilter(myRequest, response);
 33     }
 34 
 35     public void destroy() {
 36 
 37     }
 38 }
 39 
 40 // 自定义request对象
 41 class MyRequest extends HttpServletRequestWrapper {
 42     private HttpServletRequest request;
 43 
 44     public MyRequest(HttpServletRequest request) {
 45         super(request);// super必须写
 46         this.request = request;
 47     }
 48 
 49     // 对需要增强方法 进行覆盖
 50     @SuppressWarnings({"unchecked", "rawtypes"})
 51     @Override
 52     public Map getParameterMap() {
 53         // 先获得请求方式
 54         String method = request.getMethod();
 55         if (method.equalsIgnoreCase("post")) {
 56             // post请求
 57             try {
 58                 // 处理post乱码
 59                 request.setCharacterEncoding("utf-8");
 60                 return request.getParameterMap();
 61             } catch (UnsupportedEncodingException e) {
 62                 e.printStackTrace();
 63             }
 64         } else if (method.equalsIgnoreCase("get")) {
 65             // get请求
 66             Map<String, String[]> parameterMap = request.getParameterMap();
 67             for (String parameterName : parameterMap.keySet()) {
 68                 String[] values = parameterMap.get(parameterName);
 69                 if (values != null) {
 70                     for (int i = 0; i < values.length; i++) {
 71                         try {
 72                             values[i] = new String(values[i].getBytes("iso8859-1"), "utf-8");
 73                         } catch (UnsupportedEncodingException e) {
 74                             e.printStackTrace();
 75                         }
 76                     }
 77                 }
 78             }
 79             return parameterMap;
 80         }
 81         return super.getParameterMap();
 82     }
 83 
 84     @Override
 85     public String getParameter(String name) {
 86         @SuppressWarnings("unchecked")
 87         Map<String, String[]> parameterMap = getParameterMap();
 88         String[] values = parameterMap.get(name);
 89         if (values == null) {
 90             return null;
 91         }
 92         return values[0]; // 取回参数的第一个值
 93     }
 94 
 95     @Override
 96     public String[] getParameterValues(String name) {
 97         @SuppressWarnings("unchecked")
 98         Map<String, String[]> parameterMap = getParameterMap();
 99         String[] values = parameterMap.get(name);
100         return values;
101     }
102 }

 

posted @ 2019-10-11 14:41  拾月凄辰  阅读(191)  评论(0编辑  收藏  举报