Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC HttpServletResponse not overwriting cookie

Every time a different page is requested, I do the following:

    Cookie cookie = new Cookie("c1", someString); // can be {a, b, c}, for example
    cookie.setMaxAge(31556926);
    response.addCookie(cookie);

    cookie = new Cookie("c2", Integer.toString(someInt));
    cookie.setMaxAge(31556926);
    response.addCookie(cookie);

But when I visit page /page/a, then visit page /page/b (both of which goes to the same method from controller) I notice that my Cookies (from my web dev console in Chrome) has my new cookie BEFORE my old cookie... causing the old cookie to be used by the client. I've tested this also in Firefox. What am I doing wrong?

like image 220
Ben Avatar asked Feb 18 '26 06:02

Ben


1 Answers

It turned out I needed to specify the correct path for the cookie, so I just set the cookie's path to /

like image 173
Ben Avatar answered Feb 20 '26 20:02

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!