1.存cookie时将值编码
String value=URLEncoder.encode(name+":"+password, "UTF-8");
Cookie cookie=new Cookie("name:password", value);
response.addCookie(cookie);
2.取cookie时将值解码
Cookie[] cookies=request.getCookies();
for(Cookie cookie:cookies){
String cookieName=cookie.getName();
if(cookieName.equals("name:password")){
String cookieValue=URLDecoder.decode(cookie.getValue(), "UTF-8");
}
}
这样就不会有中文乱码的问题。