cookie中乱码的问题String username = request.getParameter("username"); //假设获取到的值为中文 username = URLEncode.en

      中文属于Unicode编码, 而英文属于Ascll编码, Cookie中又只能存储英文,要想向cookie中存取中文就要对中文进行编码 当向cookie中存储时,使用URLEncode类中的encode方法对文本进行转码。 当从cookie中读取时,使用URLDecode类中的decode方法进行解码 实例如下:

String username = request.getParameter("username");	//假设获取到的值为中文
username = URLEncode.encode(username);	//使用encode方法对字符串进行转码
Cookie cookie = new Cookie("username",username);
response.addCookie(cookie);	//	保存到cookie中
 
Cookie[] cookie = request.getCookie();
for(Cookie cok:cookie){
	if(cok.getName().equals("username")){
		String name = cok.getValue();
		name = URLDecode.decode(name,"utf-8");	//到此为止完成解码,name此时为中文显示
	}
 
}	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值