//写cookie操作 Cookie cooki; cooki=new Cookie("bbs_0001",String.valueOf(num));//用户ID cooki.setMaxAge(60*60*24*365);//cookie时间 cooki.setPath("/"); //根据个人的不用,在不同功能的路径下创建 response.addCookie(cooki);
//清空Cookie操作 Cookie[] cookies=request.getCookies(); try{ for(int i=0;i<cookies.length;i++){ Cookie cookie = new Cookie("bbs_0001",null); cookie.setMaxAge(0); //cookie.setPath("/");//根据你创建cookie的路径进行填写 response.addCookie(cookie); } }catch(Exception ex){ out.println("清空Cookies发生异常!"); }
添加session session.setAttribute("isSessionLogin",name); 删除session session.removeAttribute("isSessionLogin");
实例:
Cookie[] cookies = request().getCookies(); for (Cookie coo : cookies) { if ("LOGINNAME".equals(coo.getName())) { String loginName1 = coo.getValue(); if (!loginName1.equals(loginName)) { for (Cookie coo1 : cookies) { coo1.setMaxAge(0); //清空cookie response().addCookie(coo1); break; } } } }
页面jquery操作cookie
//保存到cookie if(<span style="font-family: 'Courier New'; white-space: pre-wrap;">remeber==</span>1){ $.cookie('LOGINNAME', "admin", {expires: 30, path: '/manger' }); $.cookie('LOGINPASSWORD', "123", {expires: 30, path: '/manger' }); } //删除cookie if(<span style="font-family: 'Courier New'; white-space: pre-wrap;">remeber== </span>0){ $.removeCookie('LOGINNAME'); $.removeCookie('LOGINPASSWORD'); }