• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet initialization

 
Abhishek Kumar Singh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Any body can tell me where the servlet is initialized (in the constructor or the init() method).Thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on what exactly you mean by initialization. After the constructor has been run, the object is ready to be used; so it is initialized as far as the JVM is concerned. But the developer may have put some initialization code in the init method; so as far as he is concerned, the servlet would not be initialized until init has been called.
 
hatim osman
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...
From the "servlet life cycle" perspective the following two phases are different;

1. Construction: in this phase the container merely creates an instance of your "class" by calling the constructor.
2. Initialization: here where the container grants the created instance the priviliges and benefits of becoming a servlet, and this take place in the init() method.

You may add some code in your contrctor to initalize the created instance, but you will not be able to access any servlet features as they the instance is not initalized yet.

Hatim
 
Abhishek Kumar Singh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Ulf...that's exactly the reason why there is an init() method in a servlet's life cycle...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic