grails开发之解决cookie中文报错

Version 0 cookie values are restrictive in allowed characters. It only allows URL-safe characters. This covers among others the alphanumeric characters (a-z, A-Z and 0-9) and only a few lexical characters, including -, _, ., ~ and %. All other characters are invalid in version 0 cookies.

使用java的URLEncoder和URLDecoder.

Your best bet is to URL-encode those characters. This way every character which is not allowed in URLs will be percent-encoded in this form %xx which is valid as cookie value.
So, when creating the cookie do:

1
Cookie cookie = new Cookie(name, URLEncoder.encode(value, "UTF-8"));

And when reading the cookie, do:

1
String value = URLDecoder.decode(cookie.getValue(), "UTF-8");/