日期:2014-05-16 浏览次数:20286 次
* 写cookie */ Cookie namecookie = new Cookie("name",name); Cookie passwordcookie = new Cookie("password",password); Cookie optioncookie = new Cookie("option","1"); //生命周期 namecookie.setMaxAge(60*60*24*365); passwordcookie.setMaxAge(60*60*24*365); optioncookie.setMaxAge(60*60*24*365); response.addCookie(namecookie); response.addCookie(passwordcookie); response.addCookie(optioncookie); /* * 读cookie */ Cookie[] cookies = request.getCookies(); if(cookies!=null) { String name = ""; String password = ""; String option = ""; for (int i = 0; i < cookies.length; i++) { Cookie c = cookies[i]; if(c.getName().equalsIgnoreCase("name")) { name = c.getValue(); } else if(c.getName().equalsIgnoreCase("password")) { password = c.getValue(); } else if(c.getName().equalsIgnoreCase("option")) { option = c.getValue(); } } }
?
function getcookie(name) { var cookie_start = document.cookie.indexOf(name); var cookie_end = document.cookie.indexOf(";", cookie_start); return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length))); } function setcookie(cookieName, cookieValue, seconds, path, domain, secure) { var expires = new Date(); expires.setTime(expires.getTime() + seconds); document.cookie = escape(cookieName) + '=' + escape(cookieValue) + (expires ? '; expires=' + expires.toGMTString() : '') + (path ? '; path=' + path : '/') + (domain ? '; domain=' + domain : '') + (secure ? '; secure' : ''); }
?
/// <summary> /// 写cookie /// </summary> /// <param name="name">必填,key</param> /// <param name="value">必填,value</param> /// <param name="exptime">可选,exptime,数字</param> SetCookies : function(name,value,exptime){ try{ if(arguments.length == 2) return arguments.callee(name,value,30*24*60*60*1000); var exp = new Date(); exp.setTime(exp.getTime() + exptime); document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString(); } catch(e){ throw new Error("SetCookies: " + e.message); return false; } } /// <summary> /// 读cookie /// </summary> /// <param name="name">必填,key</param> GetCookies : function(name){ try{ var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return null; } catch(e){ throw new Error("GetCookies: " + e.message); return false; } } //调用 document.getElementById('btn').attachEvent("onclick",function(){ SetCookies ("key","value"); });
?
?