在javaweb中使用Cookie经常会出现乱码问题。

解决方法:对中文进行编码和解码

存储到Cookie中时:

String str = URLEncoder.encode("你好","UTF-8");
  Cookie cookie = new Cookie("aaa", str);
  cookie.setMaxAge(600);
  response.addCookie(cookie);
  response.sendRedirect("index.jsp");

从Cookie中获取时:

Cookie[] cookies = request.getCookies();
     for(Cookie c : cookies){
      if(c.getName().equals("aaa")){
       String result = URLDecoder.decode(c.getValue(),"UTF-8");
       out.println(result);
      }  
     }

相关文章:

  • 2022-12-23
  • 2021-10-09
  • 2022-01-15
  • 2021-11-19
  • 2022-12-23
  • 2021-08-14
猜你喜欢
  • 2021-08-07
  • 2021-09-13
  • 2022-12-23
  • 2021-10-11
  • 2021-05-28
  • 2021-12-21
  • 2021-07-28
相关资源
相似解决方案