How to check whether a cookie has been set on previous visit?
In a java web application,I want to check whether a user who signs in is a returning user. How can I check if there is already a cookie that has been set on earlier login.
Set cookie when user performs log-in:
Cookie c = new Cookie("visit", "old") c.setMaxAge(3600*24*365*1000); // 1 year (for example) response.addCookie(new Cookie("visit", "old"));
Now you can check this cookie when user with new session comes to the system: request.getCookies(), then iterates over returned array and find "your" cookie. If cookie exists this is "old" user otherwise the new one.
On HttpServletRequest you have a getCookies() method that will give you an array of the cookies the client is sending with his request.