Using Cookies
Creating a new Cookie
Cookie cookie = new Cookie(“username”, name);
Setting how long a cookie will live on the client
cookie.setMaxAge(30*60);
Alive for 30*60 seconds only.
-1 makes the cookies disappear when browser exists.
Sending the cookie to the client
response.addCookie(cookie);
Getting the cookie(s) from the client request
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals(“username”)) {
String userName = cookie.getValue();
out.println(“Hello “ + userName);
break;
}
}
Note : There is only a addCookie() method, there is not setCookie() method
Cookie cookie = new Cookie(“username”, name);
Setting how long a cookie will live on the client
cookie.setMaxAge(30*60);
Alive for 30*60 seconds only.
-1 makes the cookies disappear when browser exists.
Sending the cookie to the client
response.addCookie(cookie);
Getting the cookie(s) from the client request
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals(“username”)) {
String userName = cookie.getValue();
out.println(“Hello “ + userName);
break;
}
}
Note : There is only a addCookie() method, there is not setCookie() method