解决全站字符乱码(POST和GET中文编码问题)

Posted gdwkong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决全站字符乱码(POST和GET中文编码问题)相关的知识,希望对你有一定的参考价值。

1 说明
乱码问题:
  • 获取请求参数中的乱码问题;
    •  POST请求:request.setCharacterEncoding(“utf-8”);
    •  GET请求:new String(request.getParameter(“xxx”).getBytes(“iso-8859-1”), “utf-8”);
      • Tomcat 8.5.20、Tomcat 9.0.0.M26 默认编码都是utf-8,所有不出现GET请求乱码问题,所以不需处理
  • 响应的乱码问题:response.setContextType(“text/html;charset=utf-8”)。
2、当GET请求出现乱码问题时(默认编码为ISO-8859-1时),可采用以下方法解决:
 1 package web.filter;
 2 
 3 import javax.servlet.*;
 4 import javax.servlet.annotation.WebFilter;
 5 import javax.servlet.http.HttpServletRequest;
 6 import java.io.IOException;
 7 
 8 @WebFilter(filterName = "EncodingFilter",urlPatterns = "/*")
 9 public class EncodingFilter implements Filter {
10     public void destroy() { }
11     public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
12         //处理post请求编码问题
13         req.setCharacterEncoding("utf-8");
14 
15         HttpServletRequest request = (HttpServletRequest) req;
16         //处理get请求的编码问题
17         if ( request.getMethod().equals("GET")){
18             EncodingRequest er = new EncodingRequest(request);
19             chain.doFilter(er, resp);
20         } else {
21             chain.doFilter(req,resp);
22         }
23     }
24     public void init(FilterConfig config) throws ServletException { }
25 }
 1 package web.filter;
 2 
 3 import javax.servlet.http.*;
 4 import java.io.UnsupportedEncodingException;
 5 
 6 /**
 7  * 装饰request
 8  */
 9 public class EncodingRequest extends HttpServletRequestWrapper{
10     private HttpServletRequest request ;
11     public EncodingRequest(HttpServletRequest req) {
12         super(req);
13         this.request = req;
14     }
15     public String getParameter(String s) {
16         String value = request.getParameter("s");
17         //处理编码问题
18         try {
19             value = new String(value.getBytes("iso-8859-1"),"utf-8");
20         } catch (UnsupportedEncodingException e) {
21             throw new RuntimeException(e);
22         }
23         return value;
24     }
25 }
 1 package web.servlet;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.annotation.WebServlet;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import java.io.IOException;
 9 
10 @WebServlet(name = "AServlet",urlPatterns = "/AServlet")
11 public class AServlet extends HttpServlet {
12     protected void doPost(HttpServletRequest request, HttpServletResponse response)
13             throws ServletException, IOException {
14         response.setContentType("text/html;charset=utf-8");
15         String username = request.getParameter("username");
16         response.getWriter().println(username);
17     }
18 
19     protected void doGet(HttpServletRequest request, HttpServletResponse response)
20             throws ServletException, IOException {
21         response.setContentType("text/html;charset=utf-8");
22         String username = request.getParameter("username");
23         response.getWriter().println(username);
24     }
25 }
 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 3 <html>
 4   <head>
 5     <title>$Title$</title>
 6   </head>
 7   <body>
 8   <a href="<c:url value=‘/AServlet?username=张三‘/>">点击这里</a>
 9   <form action="<c:url value=‘/AServlet‘/> " method="post">
10     用户名:<input type="text" name="username" value="李四"/>
11     <input type="submit" value="提交"/>
12   </form>
13   </body>
14 </html>

3、当只有POST请求出现乱码问题时(服务器默认编码为UTF-8时),可采用以下方法解决:

 1 @WebFilter(filterName = "EncodingFilter",urlPatterns = "/*")
 2 public class EncodingFilter implements Filter {
 3     public void destroy() { }
 4     public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
 5         //处理post请求编码问题
 6         req.setCharacterEncoding("utf-8");
 7         chain.doFilter(req,resp);
 8     }
 9     public void init(FilterConfig config) throws ServletException {}
10 }
 1 @WebServlet(name = "AServlet",urlPatterns = "/AServlet")
 2 public class AServlet extends HttpServlet {
 3     protected void doPost(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         response.setContentType("text/html;charset=utf-8");
 6         String username = request.getParameter("username");
 7         response.getWriter().println(username);
 8     }
 9 
10     protected void doGet(HttpServletRequest request, HttpServletResponse response)
11             throws ServletException, IOException {
12         response.setContentType("text/html;charset=utf-8");
13         String username = request.getParameter("username");
14         response.getWriter().println(username);
15     }
16 }

 

以上是关于解决全站字符乱码(POST和GET中文编码问题)的主要内容,如果未能解决你的问题,请参考以下文章

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

iOS 开发 post 提交数据 中文乱码 一串问号,怎么解决

get和post乱码问题解决

request和response对象如何解决中文乱码问题?

jsp里post和get的乱码解决问题

JavaWebHttp get请求乱码post请求乱码,html页面乱码jsp页面乱码,控制台tomcat日志乱码原因分析和解决方案参考: