我在servlet中设置Cookie,其值是中文的,如何在页面 通过JS读取cookie 不是乱码?
public Cookie setMsgCookie(String name, String value, String domain) {
domain = domain.substring(domain.indexOf("."));
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(43200);
cookie.setPath("/");
cookie.setDomain(domain);
return cookie;
}JS
function getCookie (name){ //获取Cookie
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr !=null) return unescape(arr[2]); return null;
}
public Cookie setMsgCookie(String name, String value, String domain) {
domain = domain.substring(domain.indexOf("."));
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(43200);
cookie.setPath("/");
cookie.setDomain(domain);
return cookie;
}JS
function getCookie (name){ //获取Cookie
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr !=null) return unescape(arr[2]); return null;
}
解决方案 »
- 一个关于WebService应用的问题。求解!
- tamcat加载web时contextloader出错
- 两个TOMCAT的初级问题
- 这个简单的问题,大家不要笑我了,谢谢大家!
- 关于jsp中:Cannot find any information on property 'X' in a bean of type 'Y'的问题~~~
- struts中的问题 The server encountered an internal error (No action instance for path /person could be created)
- 论坛开发?
- resin是不是只能做轻量级应用?
- Jboss_tomcat在Linux下不能运行JSP的问题,请提点一下,谢谢。
- 大家来看看我的网站,是实现类似于msnphoto的网上实时图像处理的
- 老问题了,关于乱码问题,小弟跪谢了!!
- 关于jstl使用的一些问题
domain = domain.substring(domain.indexOf("."));
Cookie cookie = new Cookie(name, java.net.URLEncoder.encode(value, "UTF-8"));
cookie.setMaxAge(43200);
cookie.setPath("/");
cookie.setDomain(domain);
return cookie;
}JS
function getCookie (name){ //获取Cookie
var arr = document.cookie.match(new RegExp("(^ ¦ )"+name+"=([^;]*)(; ¦$)"));
if(arr !=null) return decodeURIComponent(arr[2]); return null;
}