how do immediately see changes I made on a cookie? | Community
Skip to main content
jayv25585659
Level 8
September 25, 2025
Solved

how do immediately see changes I made on a cookie?

  • September 25, 2025
  • 3 replies
  • 863 views

so I have a javax.servlet.filter class that does a lot of things. one of the first things it does is change the value of a cookie (example: cookieX was originally set to "ABC" and I changed it to "ABC,DEF").

 

Before the browser gets a response (the filter has completed already but a Sling model is running this time), another piece of code retrieves the value of cookieX and checks if "DEF" is in the cookie. When the cookie is retrieve via the request object, I still see the original value (which is "ABC") and not the new one.

  • From what I can see, this is inherent to the nature of cookies? (that is the values will not change until the browser gets a response?)
  • Is there a workaround/fix?

Thank you.

Best answer by SantoshSai

Hi @jayv25585659,

It depends on why you need the updated cookie during the same request:

  1. Store the updated value in a request attribute
    After you update the cookie on the response, also do:

    request.setAttribute("cookieXUpdatedValue", "ABC,DEF");

    Then, in your Sling Model (or later code in the same request), check the attribute first, falling back to request.getCookies() if it’s not present.

  2. Wrap the request
    You can implement a HttpServletRequestWrapper inside your Filter that overrides getCookies() (or getHeader("Cookie")) to return the updated value. Then pass this wrapped request down the chain:

    chain.doFilter(new MyWrappedRequest(request, "cookieX", "ABC,DEF"), response);

    This way, downstream code sees the updated cookie value.

  3. Avoid relying on cookies within the same request
    If the “DEF” flag is needed only for server-side flow control, you might skip cookies altogether and rely on request attributes, thread-locals, or another context mechanism. Cookies are meant for persisting state between requests, not within one.

3 replies

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
September 25, 2025

Hi @jayv25585659,

It depends on why you need the updated cookie during the same request:

  1. Store the updated value in a request attribute
    After you update the cookie on the response, also do:

    request.setAttribute("cookieXUpdatedValue", "ABC,DEF");

    Then, in your Sling Model (or later code in the same request), check the attribute first, falling back to request.getCookies() if it’s not present.

  2. Wrap the request
    You can implement a HttpServletRequestWrapper inside your Filter that overrides getCookies() (or getHeader("Cookie")) to return the updated value. Then pass this wrapped request down the chain:

    chain.doFilter(new MyWrappedRequest(request, "cookieX", "ABC,DEF"), response);

    This way, downstream code sees the updated cookie value.

  3. Avoid relying on cookies within the same request
    If the “DEF” flag is needed only for server-side flow control, you might skip cookies altogether and rely on request attributes, thread-locals, or another context mechanism. Cookies are meant for persisting state between requests, not within one.

Santosh Sai
jayv25585659
Level 8
September 25, 2025

question please. do you know if option1 persist across multiple publishers where sticky sessions is not set/disabled?

SantoshSai
Community Advisor
Community Advisor
September 25, 2025

No, option 1 (request attribute) won’t persist across multiple publishers.
If you need the value available across requests regardless of which publisher is hit, you’ll need to rely on the cookie itself (or another mechanism that goes back through the client and load balancer).

Santosh Sai
September 25, 2025

Cara reschedule Air Asia

_

Telepon cs Air Asia (+62081944399827) 

September 25, 2025

Cara reschedule Lion air

_

Cs lion air Telepon 0819.4439.827