I have been wondering why my application has been allowing duplicate votes for a while and finally debugged the issue.
When you vote you get a cookie named “vote” and the value is the ID of the “thing” you voted on.
Cookie voteCookie = new Cookie("vote", "1901");
voteCookie.setMaxAge(60*60*24*365);
response.addCookie(voteCookie);
I checked the client (Chrome) and there is a cookie named “vote” with the value “1901”
When I then read this cookie in my Spring Controller like this:
Cookie[] cookies = request.getCookies();
I now have two cookies:
CookieName: ["vote"] Value: [""]
CookieName: ["1901"] Value: [""]
The other cookies like JSESSIONID are correct:
CookieName: ["JSESSIONID"] Value: [000046KQaPe3T8gwXVuB3zhNrON:-1]
What am I doing wrong?
Edit: Turns out there was some code that appended the old value with the new one:
voteCookie.setValue(voteCookie.getValue() + "," + cookieValue);
And this seems to be what caused the problem. The value after the “,” (comma) became another cookie with that name. I’m not sure if this is a WebSphere Application Server issue or what. But changed the delimiter to “-” and everything works!
Seems the problem was due to commas in the value of the cookie. There are some references to the javadoc in this question: Cannot get value from cookie in Java. Value contains colons
I changed the delimiter to “-” and all is good