views:

159

answers:

2

How is a session object attached to each thread of a servlet? I believe its not a ThreadLocal, so how is it attached to each servlet thread?

A: 

A JSESSIONID variable gets set in the client's cookie (or URL sometimes) and the container uses the JSESSIONID to look up the appropriate session for the given request.

Asaph
+1  A: 

It's not attached to the Servlet thread, it's attached to the HttpServletRequest. Each invocation of the Servlet is passed a HttpServletRequest and an HttpServeltResponse. So, they're just local variables to the Servlet instance -- nothing to do with the thread.

Will Hartung