//创建Cookie URLEncoder.encode 处理中文
Cookie cookie = new Cookie("UserName", URLEncoder.encode(r.getString("name").trim(), "UTF-8"));
cookie.setMaxAge(60*60*24*365); //设置1年的寿命cookie
//发送Cookie
response.addCookie(cookie);
//读取Cookie
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for (Cookie c : cookies) {
String name = c.getName().trim();//获取Cookie名称
String value = URLDecoder.decode(c.getValue().trim(), "UTF-8");//获取Cookie的值 URLDecoder.decode 处理中午
System.out.println("cookies 读取 " + name + "=" + value);
}
}
// 换一个
Cookie cookie = new Cookie("UserName", "");
cookie.setMaxAge(0);
response.addCookie(cookie);
<script>
function logout()
{
//alert("清除cookis");
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
window.location.reload(); //重load
}
</script>
tmp=tmp+"你好,"+cookisUsername+ "!<a href=\"javascript:logout();\">退出登录</a>";
发表评论