In a typical web application, when a request comes in, a filter looks for a Context object in http session. If one doesn’t exist, it creates the Context object and stores it in http session. Additionally, this Context object is also stored in the ThreadLocal object. A Servlet down the path retrieves this Context object from ThreadLocal and modifies it. When the response is returned, the filter now nulls the Context object in ThreadLocal. So when the user makes another request, will he able to see the modified Context object?
Thanks
Quadir
yes, the user will see the Context object because a reference to it was stored in the HttpSession. Even though the reference in the ThreadLocal was nulled, it will still be found in the session during the second request.
EDIT: In the OpenJDK sourcecode of ThreadLocal (from line 410) you can see the difference between ThreadLocal’s set and remove methods. Calling set(null) will leave the ThreadLocalMap entry in place with a null value whereas remove() will delete it entirely. It won’t make a difference with regard to your question, there will still be a reference to your Context object in the Session.
When I first read your question title I interpreted it differently because there was no mention of the HttpSession or clearing the ThreadLocal. Maybe this confused some of the responders. It sounded like you wanted to know if a ThreadLocal variable set in the first request (and not cleared) would still be accessible in the second request. I think the answer is that this depends on how your webserver handles threads. If there is a threadpool of 10 threads that are randomly reused you should have a 10% chance of finding the same ThreadLocal variable in the second request.