码迷,mamicode.com
首页 > 编程语言 > 详细

javaweb解决post和get请求方式中文乱码问题(通用)

时间:2017-08-30 23:43:36      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:method   res   bool   eterm   oid   get请求   const   new   charset   

 

  • 添加一个过滤器,解决乱码问题,代码如下:
  1 import java.io.IOException;
  2 import java.io.UnsupportedEncodingException;
  3 import java.util.Map;
  4 
  5 import javax.servlet.Filter;
  6 import javax.servlet.FilterChain;
  7 import javax.servlet.FilterConfig;
  8 import javax.servlet.HttpConstraintElement;
  9 import javax.servlet.ServletException;
 10 import javax.servlet.ServletRequest;
 11 import javax.servlet.ServletResponse;
 12 import javax.servlet.http.HttpServletRequest;
 13 import javax.servlet.http.HttpServletRequestWrapper;
 14 
 15 /**
 16  * 通用解决get 和 post 方式请求的中文乱码问题
 17  * @author 代码小杨
 18  *
 19  */
 20 public class EncodingFilter implements Filter {
 21     
 22     public void destroy() {
 23     }
 24     
 25     @Override
 26     public void doFilter(ServletRequest request, ServletResponse response,
 27             FilterChain chain) throws IOException, ServletException {
 28         //处理请求乱码
 29         HttpServletRequest httpServletRequest = (HttpServletRequest) request;
 30         HttpServletRequest myRequest = new MyRequest(httpServletRequest);
 31 
 32         //处理响应乱码
 33         response.setContentType("text/html;charset=utf-8");
 34         chain.doFilter(myRequest, response);
 35     }
 36 
 37     @Override
 38     public void init(FilterConfig filterConfig) throws ServletException {
 39 
 40     }
 41 }
 42 
 43 //自定义request对象
 44 class MyRequest extends HttpServletRequestWrapper{
 45 
 46     private HttpServletRequest request;
 47     private boolean hasEncode;
 48 
 49     public MyRequest(HttpServletRequest request) {
 50         super(request);
 51         this.request=request;
 52     }
 53 
 54     //对需要增强的方法 进行覆盖
 55     @Override
 56     public Map getParameterMap() {
 57         //先获得请求方式
 58         String method = request.getMethod();
 59         if(method.equalsIgnoreCase("post")){   //post方式请求
 60             try {
 61                 request.setCharacterEncoding("utf-8");
 62                 return request.getParameterMap();
 63             } catch (UnsupportedEncodingException e) {
 64                 e.printStackTrace();
 65             }
 66         }else if(method.equalsIgnoreCase("get")){  //get方式请求
 67             Map<String,String[]> parameterMap = request.getParameterMap();
 68             if(!hasEncode){
 69                 for(String parameterName:parameterMap.keySet()){
 70                     String[] values = parameterMap.get(parameterName);
 71                     if(values!=null){
 72                         for (int i = 0; i < values.length; i++) {
 73                             try {
 74                                 values[i] = new String(values[i].getBytes("ISO-8859-1"),"utf-8");
 75                             } catch (UnsupportedEncodingException e) {
 76                                 e.printStackTrace();
 77                             }
 78                         }
 79                     }
 80                 }
 81                 hasEncode =  true;
 82             }
 83             return parameterMap;
 84         }
 85         return super.getParameterMap();
 86     }
 87 
 88     @Override
 89     public String getParameter(String name) {
 90         Map<String,String[]> parameterMap = getParameterMap();
 91         String[] values = parameterMap.get(name);
 92         if(values==null){
 93             return null;
 94         }
 95         return values[0];
 96     }
 97 
 98     @Override
 99     public String[] getParameterValues(String name) {
100         Map<String,String[]> parameterMap = getParameterMap();
101         String[] values=parameterMap.get(name);
102         return values;
103     }
104 }
  • web.xml文件中配置过滤器
 <filter>
      <filter-name>EncodingFilter</filter-name>
      <filter-class>com.**.web.filter.EncodingFilter</filter-class>
  </filter>
  
  <filter-mapping>
      <filter-name>EncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

 

javaweb解决post和get请求方式中文乱码问题(通用)

标签:method   res   bool   eterm   oid   get请求   const   new   charset   

原文地址:http://www.cnblogs.com/id-666/p/7455701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!