servlet instances
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   B_Som
Posted On:   Wednesday, August 4, 2004 07:01 AM

how many servlet instances will be created when number of requests comes from a client?

Re: servlet instances

Posted By:   Vikash_Ranjan  
Posted On:   Wednesday, August 11, 2004 10:55 PM

By default a servlet is multi-threaded i.e it handles multiple requests concurrently.
Typically,there is only one instance of your servlet, no matter how many client requests are in process. That means that at any given moment, there may be many threads running inside the service() method of your solo instance, all sharing the same instance data.

Hence be careful to make your servlet classes thread-safe....preferably don't have any instance variables in your servlet class. Make them local varibales of the service method, because these local variables reside on the Java stack.(In the JVM, each thread is awarded its own Java stack, hence the safety)

But if your servlet implements SingleThreadModel, things are different. SingleThreadModel :-This empty interface tells the web server to only send one client request at a time into your servlet instance. If the target servlet is flagged with this interface, the servlet programmer is guaranteed that no two threads will execute concurrently the service method of that servlet. This guarantee is ensured by maintaining a pool of servlet instances for each such servlet, and dispatching each service call to a free servlet.

Re: servlet instances

Posted By:   vinay_shukl  
Posted On:   Wednesday, August 11, 2004 03:32 AM

only one all the request are fullfilled by ths one instance

Re: servlet instances

Posted By:   Anonymous  
Posted On:   Thursday, August 5, 2004 01:59 PM

According to my knowledge,from developer perspective,the servlet instance will be created only once.There will be an option in the server(or more specifically the servlet conatiner)when to creat instance, though it is once.
Usually,when developers write a servlet,servlet is loaded and intialized and its instance will be created automatically,when the server is ON.
The other way is,a servlet is written by developer.when the client requests server for a servlet for the first time by any client, servlet is loaded and intialized and its instance will be created.from the very next request,the instance will be there already.
Either of the cases it is intialized only once.When ever a request comes to the server,server calls a thread from worker threads(thread pool maintained by server conatainer) in server container.This thread executes the service method of the servlet for which you requested.But your question says-- when number of requests comes from a client.From clients perspective,if you request for a servlet then thread executes on the service method of that servlet instance which is already created.so,nothing like that for ever request there will be a new instance creation.but different requests(for different servlets which do different tasks--like servlet A,servlet B,servlet C..) go to the server (from the same client or different client,it doesn't matter),then different threads will execute on different service methods of different servlet-instances which were created already.So,evidently,a servlet instance will not be created for every request you do whether client is same or not.Any issues,glad to discuss.thanks

Re: servlet instances

Posted By:   tech_tech  
Posted On:   Wednesday, August 4, 2004 10:50 PM

sounds like a perfect interview question.
depends on the contaner setting. dont worry tomorrow it will be different questions.
About | Sitemap | Contact