Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Precisely when is init() called?

 
Michael Beauchamp
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep finding documentation indicating that a servlet's init method should be called before handling any requests. But I can't find anything that states exactly when the init is called. The behavior of my servlet indicates that it is only initialized after it receives its first request. It definitely finishes the init() before handling that first request, but it got me wondering how I could make sure the init() method is called before I need to handle that first request.

I have very little experience with servlets, so I may be missing something fundamental. I just wonder how one would handle a servlet that has a resource-intensive initialization process so that it doesn't create a performance hit for the first user.

Thanks in advance for any info.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gets called when the servlet is put into service.

Depending on your container and your configuration (look up "load-on-startup") your servlet will either load as soon as your application is deployed or it will take the lazy approach and wait until the first request for that particular servlet.
 
Michael Beauchamp
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to emphasize the point:

but it got me wondering how I could make sure the init() method is called before I need to handle that first request.


This is never something you will have to worry about. init() will always be called prior to the servlet servicing a request.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Beauchamp:
how I could make sure the init() method is called before I need to handle that first request.


You do not have to do anything.The container will do this as part of servlet life cycle.

Put system.out.print statement in init and your overridden doXXX method to see what is called first.
 
Rajesh Agarwal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whenever the client enters the URL the request is mapped to a particular servlet. The server then laods the servlet into the memory and calls the init method. This is called only once during the first servlet call. For subsequest requests only the service is called.

But if <load-on-start-up> attribute of the <servlet> tag is set in the web.xml, then this initialization is done when the server starts up.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajesh Agarwal:
whenever the client enters the URL the request is mapped to a particular servlet. The server then laods the servlet into the memory and calls the init method. This is called only once during the first servlet call. For subsequest requests only the service is called.

But if <load-on-start-up> attribute of the <servlet> tag is set in the web.xml, then this initialization is done when the server starts up.


... which is precisely what Ben Souther wrote.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:


... which is precisely what Ben Souther wrote.


Me too.

Wait....
I am Ben Souther.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic