• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic