Cookie

2018-03-23  本文已影响0人  LIsPeri

获取Cookies
var Cookie = {
getCookie: function(Name){
var search = Name + "="
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search)
if(offset != -1)
{
offset += search.length
end = document.cookie.indexOf(";", offset)
if(end == -1) end = document.cookie.length
return decodeURIComponent(decodeURIComponent(document.cookie.substring(offset, end)).replace(/+/g,' ').replace(/\/g,'\\'));
}
else return ""
}
},
//过期时间
setCookie:function(name,value){
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days2460601000);
document.cookie = name + '=' + value +";expires="+exp.toGMTString();
}
}
一.向客户程序发送cookie
1.创建cookie对象
Cookie c=new Cookie(" userle","landr");
2.设置最大的时效
c.setMaxAge(606024); //一天,如果设置为0则删除cookie
3.向客户端添加Cookie
response.addCookie(c);

二,从客户端读取cookie
1.获取Cookie数组
2.对数组进行循环,调用Cookie的getName方法,获取具体的cookie的值
Cookie cookies[]=rsquest.getCookie();
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
Cookie c=cookies[i];
if("userle".equals(c.getName())){
System.out.println(c.GetName());
}
}
}

上一篇下一篇

猜你喜欢

热点阅读