1、创建
Cookie cookie = new Cookie("username", "my name") ; cookie.setMaxAge(60 * 60 * 24); // 有效时间,单位 s response.addCookie(cookie); // 发送cookie 至客户端
2、读取
Cookie cookies[] = request().getCookies(); Cookie cookie = null; if(cookies != null){ for(int i = 0; i < cookies.length; i++){ cookie = cookies[i]; out.println(cookie.getName()); out.println(cookie.getValue()); } }
3、删除
Cookie cookies[] = request().getCookies(); Cookie cookie = null; for(int i = 0; i < cookies.length; i++){ cookie = cookies[i]; if(cookie.getName().equals("password")){ cookie.setMaxAge(0); // 让该cookie过期 即可删除 response.addCookie(cookie) ; } }