cookie存入中文报错出现问题

本文介绍了解决中文字符在Cookie中存储的问题,通过使用URLEncoder.encode方法对中文进行编码,确保了Cookie可以正确设置并被浏览器接受。同时提供了如何在前端解码这些已编码的中文字符的方法。

我们在使用中文存入cookie是会出现如下错误

严重: Servlet.service() for servlet [com.bjsxt.servlet.CheckIdentity] in context with path [/mechanical_spring_webapp] threw exception
java.lang.IllegalArgumentException: Control character in cookie value or attribute.

是因为在

Cookie cookie=new Cookie("curr_username",name);
cookie.setPath("/");
response.addCookie(cookie);

中name包含了中文,如要解决需要将中文进行编码String name=URLEncoder.encode(name,"UTF-8");

Cookie cookie=new Cookie("curr_username",name);
cookie.setPath("/");
response.addCookie(cookie);

就能成功加入到cookie中,在界面显示是由于经过编码,显示的是utf-8格式的,在页面显示中文就需要对其进行解码

var name=document.cookie;

decodeURI(name);

name显示就是中文了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值