File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Steve Mutanson
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Steve Mutanson
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet engine is allowed to remove the servlet instance when it wants to - but should always call destroy() first.
Bill
 
Dasun Perera
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic