This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invocation init()

 
Lalitha Perumal
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

i had a discussion with my friend, abt init method

I said :
Init will be executed for the first request by the first client.

He said :
Init will be executed when the class is loaded.


Can anyone please clear us.
 
Pranav Shukla
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There servlet specification doesn't state when exactly init method should be called. But it will be called surely before servlet can serve to any client's request.

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.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you partially are right and he is also right. Normally the servelt is loaded in container when the first request is received. But we can change this behavior using <load-on-startup> in DD by setting positive interger value, to load the servlet at startup.

Thanks
 
vidya sagar
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
in DD


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 ]
 
Lalitha Perumal
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

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
or
2.when the class is loaded
 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lalitha,
I think Narendra & Vidya Sagar has already given much details on when init fires. If question is given like that somewhere,.. you cannot find two answers of that sort which are ambiguous atelast not in scwcd exam. Questions and answers will be very clear.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lalitha MP:
Hi all,

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
or
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 ]
 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
Intersting but what is this mean?
"There are 10 kinds of people in this world; those who understand binary and those who don't."
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rizwan Mohammad:
Paul,
Intersting but what is this mean?
"There are 10 kinds of people in this world; those who understand binary and those who don't."


Exactly what it says. There are (two) kinds of people...
 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
Then what is 10 kinds of people? Is that not 2 kind of people?
Exactly what it says. There are (two) kinds of people...
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
10 in binary = 2 in decimal
 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
That was great. I learned something today. So, i am a kind of person who doesnt know the binary! Thanks for your explanation.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Paul and Rizwan,

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.

Thanks
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I stronly belive, in general, the init method is executed when the class is loaded.


Me too. Always safer. Why to wait until first request. (Who know whether it will come)
 
Lalitha Perumal
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm now cleared from the dicussion with you all.

Thank you very much for that.
 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Narendra,
Servlet spec says servlet should be initialized(should call init) before it serves the first request. That is the only thing guaranteed by spec. Rest is up to container to implement. You may be correct. , most times it executes init when class is loaded.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic