- 后端创建cookie
Cookie userCookie=new Cookie("userCookie",cookValue); userCookie.setMaxAge(7*24*60*60); //设置cookie的最大生命周期为一周 userCookie.setPath("/"); //设置路径为全路径(这样写的好处是同一项目下的页面都可以访问该cookie) response.addCookie(userCookie); //response是HttpServletResponse类型
- 后端获取cookie并销毁
//获取cookie Cookie[] cookies=request.getCookies(); for(Cookie cookie: cookies){ cookie.setMaxAge(0); cookie.setPath("/"); //路径一定要写上,不然销毁不了 response.addCookie(cookie); }