• 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

Precisely when is init() called?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:


... which is precisely what Ben Souther wrote.



Me too.

Wait....
I am Ben Souther.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic