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

Posted 代码小杨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaweb解决post和get请求方式中文乱码问题(通用)相关的知识,希望对你有一定的参考价值。

 

  • 添加一个过滤器,解决乱码问题,代码如下:
  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请求方式中文乱码问题(通用)的主要内容,如果未能解决你的问题,请参考以下文章

Javaweb 请求正文中中文编码问题

Get请求,Post请求乱码问题解决方案

详解get请求和post请求参数中文乱码的解决办法

get请求和post的请求的乱码解决

Javaweb的get请求乱码解决

javaweb 解决jsp中${}传递中文值到后端以及get请求中文乱码的问题