Issue
I have two servlets, one to perform registration of the user, and another one for log already register user. Each of this servlets send a cookie to user:
String username = request.getParameter(username);
Cookie user_cookie = new Cookie("userCookie", username);
user_cookie.setMaxAge(60 * 60); //1 hour durability cookie
response.addCookie(user_cookie);
(request and response are HttpServletRequest and HttpServletResponse types). After registration, user will be automatically logged into system. Now if a client logs into system (and receives a cookie), then the same client logout and register a new user (then receive another cookie, but as shown in the code, with the same name), user_cookie file, will be overwrite or not?
Solution
Yes it will be overriden.
Old User
String username = request.getParameter(OldUser);
Cookie user_cookie = new Cookie("userCookie", OldUser);
user_cookie.setMaxAge(60 * 60); //1 hour durability cookie
response.addCookie(user_cookie);
New User
String username = request.getParameter(NewUser);
Cookie user_cookie = new Cookie("userCookie", NewUser);
user_cookie.setMaxAge(60 * 60); //1 hour durability cookie
response.addCookie(user_cookie);
Note: New User Name Not Equal to Old User Name.
Answered By - JNL