aspose file tools*
The moose likes Servlets and the fly likes Precisely when is init() called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Precisely when is init() called?" Watch "Precisely when is init() called?" New topic
Author

Precisely when is init() called?

Michael Beauchamp
Greenhorn

Joined: Mar 03, 2005
Posts: 23
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Michael Beauchamp
Greenhorn

Joined: Mar 03, 2005
Posts: 23
Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
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
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
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.


Ping & DNS - my free Android networking tools app
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Ulf Dittmer:


... which is precisely what Ben Souther wrote.


Me too.

Wait....
I am Ben Souther.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Precisely when is init() called?