It's not a secret anymore!
The moose likes Servlets and the fly likes Thread in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread in Servlet " Watch "Thread in Servlet " New topic

Thread in Servlet

priya shankar
Ranch Hand

Joined: May 12, 2003
Posts: 51
Hi all
I have one servlet ..i am creating an instance and i am accessing it...
what will happen if another user tries to access it..(whether it uses intance pooling or multithread)
If it uses pooling ,another user is going to get an instance from pool(a copy of instance as per my understading) if he finishes his work and leave will destroy() be called...if so won't it affect the original object..
Also i need a detailed explanation Pls
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13024
The servlet container (such as Tomcat) manages a single instance of your servlet. It is created the first time a request for that servlet occurs and it stays in memory. The destroy method gets called only when the servlet engine is shutting down or needs the space. There is no pool of servlet instances.
The servlet container creates a new Thread for each request and many Threads may be running the same code "at the same time." User specific data is unique to that Thread.
Therefore you have to change your programming habits that you may have used for single user applications. You can't use instance variables for user specific data.
Many discussions here have touched on the problems, so browse around.
I agree. Here's the link:
subject: Thread in Servlet
It's not a secret anymore!