The following is from Servlet spec. (page 26) first paragraph.
After the servlet object is instantiated, the container must initialize the servlet before
it can handle requests from clients.
So container might choose to instantiate,initialize the servlet instance when the webapp is deployed or it might differ the instantiation/initialization till the first client asks for service.
Init will be executed for the first request by the first client.
Statement is correct iff Specified class is not given load on start up
Init will be executed when the class is loaded.
When anybody ask U when Init will be executed.....its wise answer
(because it is applicable whether <load on start up> is used in DD R not)
[ November 09, 2005: Message edited by: vidya sagar ]
Originally posted by Lalitha MP:
Thanks for your reply.
When I am asked 'When init() will be invoked', which is the most suitable answer, Is it
1.for the first request
2.when the class is loaded
Neither answer is suitable. The only guarentees that you have with regards to when init() is called is that it will be called before the service method is run for the first time, and after the class is loaded and the constructor is called.
[ November 09, 2005: Message edited by: Paul Bourdeaux ]
Thanks for the great discussion. From the Servlet Spec. It is clear that the init method must completed before the servlet execute it's service method. But I didn't find when the init method is executed.
I understand that the init method is executed when the constructure of the sevlet class on executed. i.e. when the class is loaded. I did not found the exect statement about when the init method is executed. So, I think it is container dependent , when to execute the init method, at the time of loading the class or deffered to first request to save some resources.
But I stronly belive, in general, the init method is executed when the class is loaded.