Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: Session Tracking


Message #1 by "shaji ravindran" <sjr_r@y...> on Wed, 20 Jun 2001 08:12:40
shaji ravindran wrote:
> 
> Hi,
> Iam facing a problem in session tracking,when many users request for
>  the same resource at a time,like submitting the form,the response get
> mixed up and  the response intended to a browser goes to another
> browser.Could anybody tell me why this happens and how to fix it.We are
>  using HttpSession object to track the session.

This doesn't sound like a session problem, it sounds like a threading problem.
Servlet classes run in a multi-threading environment, so they must be
implemented to be thread safe. That does not mean the servlet must implement
Runnable or extend Thread. It means you must code the servlet so that multiple
requests can run in the servlet at the same time. Here are some tips on how to
do that:

- do NOT use member variables to hold references that are request specific. In
your particular case, it sounds like you have a class member variable holding
the reference to the output stream. Thus, all the threads in your servlet end up
sending output to the same output stream. Use method local variables for all
request specific information

- If you must use member variables, use them only for read-only information,
information that is the same for every request.

- If you must use member variables for read-write information (for example, a
request counter), protect access to these variables using synchronized methods
or synchronized blocks of code.

- If the servlet uses external resources (file, etc.) you must alos protect
access to the resource so that simultaneous requests don't try to use the
resource simultaneously.

  Return to Index