Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Init and Destroy Explicitly

 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Can we explicitly call Servlet init() or Destroy() method. If possible please tell me how is this possible.

Thanks,
Rahul
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The init and destroy methods are there for the servlet container to use. You can certainly call them but it would make no sense to do so.
What is it that you think you could accomplish?
Bill
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William,

I was just curious as this was asked to in the interview. and can yopu please through some more light on this as when init() will be explicitly called, I mean how can we perform this thing.

Thanks,
Rahul
 
Scott Duncan
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Init is called by the servlet container when the container (init)ially loads the servlet (e.g. when the app server instance is started or the application is deployed and restarted). From this you can guess what destroy does. This is why Mr. Brogden asked what you hope to accomplish by doing this? Generally, these methods perform application specific tasks in relation to start up and destruction like load static data from a database and store it in the application context. If you have a need for this data, you would put it into the init method. Since this has already been done, there would be no need to call it again. It's best to leave these methods for startup and destructrion as their name imply. Anything else you should create a new method in the servlet or even better implement it in a utility class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic