java的response对象添加Cookie报错:An invalid character [34] was present in the Cookie value

在jsp页面中,我的java代码如下:

    String json = "{\"username\":\"prince\",\"password\":\"123456\"}";
    Cookie cookie = new Cookie("user", json);
    response.addCookie(cookie);

服务器报错:
java的response对象添加Cookie报错:An invalid character [34] was present in the Cookie value_第1张图片
解决:使用URLEncoder进行转化:

    String json = "{\"username\":\"prince\",\"password\":\"123456\"}";
    Cookie cookie = new Cookie("user", URLEncoder.encode(json, StandardCharsets.UTF_8));
    response.addCookie(cookie);

你可能感兴趣的:(踩坑日记,cookie,json,java,jsp)