File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Invocation init() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Invocation init() " Watch "Invocation init() " New topic
Author

Invocation init()

Lalitha Perumal
Greenhorn

Joined: Apr 22, 2005
Posts: 11
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

Joined: Oct 05, 2005
Posts: 64
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.


Pranav Shukla
SCJP, SCWCD, SCBCD
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
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


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580

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

Joined: Apr 22, 2005
Posts: 11
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

Joined: Sep 02, 2005
Posts: 445
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.


Rizwan
SCJA, SCJP, SCWCD, SCBCD, SCDJWS.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
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 ]

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445
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

Joined: May 24, 2004
Posts: 783
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

Joined: Sep 02, 2005
Posts: 445
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

Joined: May 24, 2004
Posts: 783
10 in binary = 2 in decimal
Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445
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

Joined: Dec 04, 2004
Posts: 950
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

Joined: Nov 15, 2004
Posts: 1026

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)


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Lalitha Perumal
Greenhorn

Joined: Apr 22, 2005
Posts: 11
Hi all,

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

Thank you very much for that.
Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invocation init()