cookie的中文乱码问题【URL编码解码】

先搞明白为什么会乱码,为什么要转码:

在tomcat 8 之前,cookie中不能直接存储中文数据。需要将中文数据转码,一般采用URL编码(%E3)。
在tomcat 8 之后,cookie支持中文数据。特殊字符还是不支持(比如空格),建议使用URL编码存储,URL解码解析。

编码解码前后字符如下表所示:

编码前 十进制数字、汉字
编码后 十六进制数字、英文 解码前 十六进制数字、英文 解码后 十进制数字、汉字

 

 

 

 

 

 

浏览器与服务器交互过程如图所示:

cookie的中文乱码问题【URL编码解码】

实例代码如下:

servlet中,URL编码解码的的主要代码如下:

 1 import java.net.URLDecoder;
 2 import java.net.URLEncoder;
 3 //--------省略若干代码-----------
 4 
 5 String str_date = sdf.format(date);//获取当前时间
 6 System.out.println("编码前:"+str_date);
 7 //URL编码
 8 str_date = URLEncoder.encode(str_date,"utf-8");
 9 System.out.println("编码后:"+str_date);
10 
11 //--------省略若干代码-----------
12 
13 String value = c.getValue();//获取cookie的value
14 System.out.println("解码前:"+value);
15 //URL解码
16 value = URLDecoder.decode(value, "utf-8");
17 System.out.println("解码后:"+value);
18 
19 //--------省略若干代码-----------

全部代码如下:

cookie的中文乱码问题【URL编码解码】cookie的中文乱码问题【URL编码解码】
 1 import java.io.IOException;
 2 import java.net.URLDecoder;
 3 import java.net.URLEncoder;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.annotation.WebServlet;
 9 import javax.servlet.http.Cookie;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 
14 /**
15  * Types
16  * @author dandelion
17  * @time 2019年3月7日上午10:03:02
18  * @作用 cookie记录上次访问时间
19  */
20 @WebServlet("/CookieTest")
21 public class CookieTest extends HttpServlet {
22     private static final long serialVersionUID = 1L;
23     protected void doGet(HttpServletRequest request, HttpServletResponse response) 
24             throws ServletException, IOException {
25         response.setContentType("text/html;charset=utf-8");
26         response.setCharacterEncoding("UTF-8");
27         boolean flag=false;//没有name是lastTime的cookie
28         Cookie[] cs = request.getCookies();
29         if(cs!=null&&cs.length>0){
30             for(Cookie c:cs){
31                 String name = c.getName();
32                 if("lastTime".equals(name)){
33                     flag=true;//有name是lastTime的cookie
34                     Date date = new Date();
35                     SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd HH:mm:ss");
36                     String str_date = sdf.format(date);
37                     System.out.println("编码前:"+str_date);
38                     //URL编码
39                     str_date = URLEncoder.encode(str_date,"utf-8");
40                     System.out.println("编码后:"+str_date);
41                     c.setValue(str_date);
42                     c.setMaxAge(30);
43                     response.addCookie(c);
44                     //相应数据
45                     String value = c.getValue();
46                     System.out.println("解码前:"+value);
47                     //URL解码
48                     value = URLDecoder.decode(value, "utf-8");
49                     System.out.println("解码后:"+value);
50                     response.getWriter().write("<h1>欢迎回来,您上次访问时间是:"+value+"</h1>");
51                     break;
52                 }
53             }
54         }
55         if(cs==null||cs.length==0||flag==false){
56             Date date = new Date();
57             SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月DD HH:mm:ss");
58             String str_date = sdf.format(date);
59             str_date = URLEncoder.encode(str_date, "utf-8");
60             Cookie c = new Cookie("lastTime", str_date);
61             c.setMaxAge(300);
62             response.addCookie(c);
63             response.getWriter().write("<h1>欢迎你,这是你首次登录</h1>");
64         }
65     }
66     protected void doPost(HttpServletRequest request, HttpServletResponse response) 
67             throws ServletException, IOException {
68         doGet(request, response);
69     }
70 
71 }
View Code

控制台输出如下:

1 编码前:2019年03月07 14:28:24
2 编码后:2019%E5%B9%B403%E6%9C%8807+14%3A28%3A24
3 解码前:2019%E5%B9%B403%E6%9C%8807+14%3A28%3A24
4 解码后:2019年03月07 14:28:24

浏览器显示如下:

cookie的中文乱码问题【URL编码解码】

说明:

高亮说明

日期:2019-03-07 14:35:24