• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need for init() method

 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a question why in the KS book about the need for the init() ... that is why do we need the init() method even after the constuructors are executed??...
i guess the need is something related to the servlet context and config availability ..
could someone explain in short ... so that i can learn more about it???
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do we need the init() method even after the constuructors are executed??...


init(ServletConfig) is the life cycle method of servlet and is called by the web container on the servlet.You cannot call it yourself at your own will to get the servlet config object.so you have to wait for this method to be called if you want to use ServletConfig object.

Thanks,
Raj
 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right... so that is the only purpose of the init() ?? i.e to give access to the servlet config and context??
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and then you can initalize your servlet further using the information from Servlet config or Servlet context.
Note still we have chance to initialize the servlet as service method will not run unless init method is complete.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!