cookie不能保存中文,会报Control character in cookie value之类的exception。刚学cookie,老是卡在这儿,总体来说,就是要对cookie的value进行编码,解码。关键代码如下

1:setcookie:

String value = URLEncoder.encode(uname, "utf-8");//关键

Cookie cookie = new Cookie("usersname", value);

cookie.setMaxAge(60);

response.addCookie(cookie);

2:getcookie:

Cookie cookies[] = request.getCookies();

if(cookies != null&&cookies.length>0){

for(Cookie c : cookies){

if(c.getName().equals("usersname")){

usersname = URLDecoder.decode(c.getValue(), "utf-8");

}

}

}