ThreadLocal class enables the sharing of a variable in a statically way across a whole Java process by attaching this variable to a single Thread (p.e.
Thread.currentThread()). This is specially useful to make accesible values recovered in filter layer to the other ones.
Think about database logging of HTTP requests or responses. Note that this is the unique point to retrieve, for instance, complete signed requests or responses. All this process (request and response) is executed on a single thread, so
ThreadLocal mechanism can be used to share the request content with the DAO layer. By doing a
threadLocal.set(request) on Filter class and a
threadLocal.get() on DAO class the described functionality is achieved.
And here is where different behaviours arrive. WebSphere 6 doesn’t clean servlet pooled threads after execution while Weblogic 10 does. So, ThreadLocal variable must be cleared in WebSphere for every new request!
Typically is discouraged the use of this tecnique in managed environments, but in many cases the best should be the simpliest.
Reference – IBM – Threading lightly.