Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.
SOLVED

how do immediately see changes I made on a cookie?

Avatar

Level 9

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

AEM BlogsLinkedIn


View solution in original post

6 Replies

Avatar

Correct answer by
Community Advisor

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

AEM BlogsLinkedIn


Avatar

Level 9

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

Avatar

Community Advisor

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

AEM BlogsLinkedIn


Avatar

Level 1

Cara reschedule Batik Air

_

Cs batik air Telepon 081944399827

Avatar

Level 1

Cara reschedule Air Asia

_

Telepon cs Air Asia (+62081944399827) 

Avatar

Level 1

Cara reschedule Lion air

_

Cs lion air Telepon 0819.4439.827