1、设置一个Cookie,与要删除的Cookie同名,并将有效时间设置为0:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie cookie1=new Cookie("password1","123456"); cookie1.setMaxAge(0); response.addCookie(cookie1); }
可以看出响应中该Cookie的存活时间为0。
2、检验Cookie是否已经被删除:
运行程序对Cookie进行遍历:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie [] cookies=request.getCookies(); if(cookies!=null){ for (Cookie c:cookies){ String name=c.getName(); String value=c.getValue(); System.out.println(name+" "+value); } } }
无论是控制台还是抓包结果,都证明Cookie已经被覆盖(删除)。