Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes init() called for only once for one servlet instance ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "init() called for only once for one servlet instance ?" Watch "init() called for only once for one servlet instance ?" New topic
Author

init() called for only once for one servlet instance ?

Steve Mutanson
Ranch Hand

Joined: Apr 15, 2003
Posts: 67
1. Is init() method called for ONLY once for a servlet ? In other words, before the servlet instance is created, server calls init() ?
2. Under what circumstance will server create another servlet instance for the same Servlet ? (Forget the case when two URI map to the same servlet class). Is it when the server finds that there is no request for this servlet for a certain amount of time, then it garbage colletc this instance, then if there comes up with a new client request, it creates another new servlet instance ? Is it how it works ?
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Two situations I can think of:
1- your servlet implements SingleThreadModel
2- your web app is distributed across several JVMs... you could have one instance per JVM.
Steve Mutanson
Ranch Hand

Joined: Apr 15, 2003
Posts: 67
Originally posted by Jessica Sant:
Two situations I can think of:
1- your servlet implements SingleThreadModel
2- your web app is distributed across several JVMs... you could have one instance per JVM.

Jessica, I am not sure if I understand you correctly. Do you mean "init() is called for only once for one servlet instance" only if it is under the two conditions you just mentioned ?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Steve, here is the part of your question that Jessica responded to.
2. Under what circumstance will server create another servlet instance for the same Servlet ?


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Steve Mutanson
Ranch Hand

Joined: Apr 15, 2003
Posts: 67
Originally posted by Bosun Bello:
Steve, here is the part of your question that Jessica responded to.
2. Under what circumstance will server create another servlet instance for the same Servlet ?

So, this servlet instance can always persist in the server forever ? Is there garbage collection for it if it is not accessed for a long long time ?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
The servlet engine is allowed to remove the servlet instance when it wants to - but should always call destroy() first.
Bill
Dasun Perera
Greenhorn

Joined: Jul 09, 2002
Posts: 13
If your servlet implemets SingleTreadedModel interface there will be one instance of your class for each request. Then the init method will be called at each time.
Otherwise there can be atmost just one instance of your servlet per JVM. The init method is called just before a servlet is placed into service by the servlet container. Destry is called just before it is taken out of service. As far as I know Servlet container may take a servlet out of service. But before doing so it will call destroy method.Them it will be entitled for garbage collection. And Servlet Container might take the servlet back into action. But before that it will call init method.
Therefore there is no gurantee that a init() method is called just once. But followings are definitely guranteed(assuming server doesn't crash).
1.After putting a servlet into action init() is called.
2. Before stopping it destroy() is called.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init() called for only once for one servlet instance ?
 
Similar Threads
how many instances of my servlet class are created?
when init() is being called?
Instantiating Servlet
when is the destroy method called?
when init() method is called ?