wood burning stoves 2.0*
The moose likes Servlets and the fly likes Calling Init and Destroy Explicitly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling Init and Destroy Explicitly" Watch "Calling Init and Destroy Explicitly" New topic

Calling Init and Destroy Explicitly

Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425

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


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12756
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?
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425

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.

Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
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.

No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
I agree. Here's the link: http://aspose.com/file-tools
subject: Calling Init and Destroy Explicitly
Similar Threads
call detroy in init
overwrite the init() method in servlet?
Init() & destroy() in jsp
can we call destroy method in servlet service method?
servlet doubt