Java creates cookie and removes cookie

  • 2020-05-12 03:01:39
  • OfStack

// write cookie operation
Cookie cooki;
cooki = new Cookie (" bbs_0001 ", String valueOf (num)); / / user ID
cooki. setMaxAge (60 * 60 * 24 * 365); / / cookie time
cooki. setPath ("/"); // created in the path of different functions according to personal usage
response.addCookie(cooki);
// clear the Cookie operation
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 ("/"); // fill in according to the path you created cookie
response.addCookie(cookie);
}
}catch(Exception ex)
{
out.println(" empty Cookies! );
}
Add session
session.setAttribute("isSessionLogin",name);
Delete session
session.removeAttribute("isSessionLogin");

Related articles: