Tuesday, January 12, 2016

Cookie

Server sends the cookie to client and client sends it back with each subsequent request. Session cookies vanishes when the client's browser quits, but it is possible to set set cookies lifetime.

Creating a new cookie

Cookie cookie=new Cookie("username","parvez");

Setting time to live

cookie.setMaxAge(30);

Sending cookie to client

response.addCookie(cookie);

Getting cookie from client's request

Cookie[] cookies=request.getCookies();
for(int i=0;i<cookies.length;i++
 {
  Cookie cookie=cookies[i];
  out.println(cookie.getName()+" "+cookie.getValue());
 }

No comments:

Post a Comment