• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When is a Servlet class loaded and instantiated?

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm passed through the Servlet chapter in HFSJ but I think I didn't get how the Servlet class is first loaded. I get the impression that the idea of the Servlet class being loaded on the first deploy/startup of the container is somehow vendor-dependent.

Also, is possible for the init() method not to be called when the Servlet is instantiated? Thanks!
 
Tarun Yadav
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AFAIK, the specification doesn't mandate when the container should load a servlet ( unless using <load-on-startup> ).

So the container can load and instantiate the class whenever it needs to. By using <load-on-startup>, you can be sure that your servlets will be loaded when the app is starting up and be available. Otherwise, the order is pretty much random and on a need-to basis. A servlet may be ready and waiting for requests, or it may be loaded and initialized only when a request comes for it.

And I'm not very sure about this but yes, I don't think it's necessary for the init() to be called immediately when the servlet is instantiated. But it must be called before the servlet can service any requests.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tarun

Thank you very much. I think I'm very satisfied with your explanation. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic