What happens if you call destroy() from init() in java servlet?

destroy() gets executed and the initialization process continues. It is a trick question in servlets interview.

In java servlet, destroy() is not supposed to be called by the programmer. But, if it is invoked, it gets executed. The implicit question is, will the servlet get destroyed? No, it will not. destroy() method is not supposed to and will not destroy a java servlet. Don’t get confused by the name. It should have been better, if it was named onDestroy().

The meaning of destroy() in java servlet is, the content gets executed just before when the container decides to destroy the servlet. But if you invoke the destroy() method yourself, the content just gets executed and then the respective process continues. With respective to this question, the destroy() gets executed and then the servlet initialization gets completed.

Have a look at this java servlet interview question: Servlet Life Cycle – Explain, it might help you to understand better.

This Servlet tutorial was added on 27/05/2008.



Comments on "What happens if you call destroy() from init() in java servlet?" Tutorial:

  1. Prasanth says:

    this is good info. I have a question- This is not related to JSP. This is about threads. We have Object.wait() & Thread.sleep(). Why can not we have both in a single class or vice versa? or why wait() is in a Object class?

    • suv says:

      because you wait() on (as well as notify()) an Object (which can also be a Thread). whereas only a Thread can sleep().

      Its like they could have made wait() a thread specific method, but then the developer would have to take care of its reference everywhere else so that other objects can see it waiting. Instead wait() is called on the monitor which is a part of Object class.

  2. Magesh says:

    Superb joe keep going man….

  3. Shampa says:

    Very nice documentation. My request to u is to Add Struts documentation also.

  4. Niraj says:

    Hi Joseph,

    These are great concepts on Java and J2ee.If can share your Struts and Hibernate [ hope you have ...must be]experience then it will be a great pleasure.


  5. Bhanu says:

    the wait method id wait for one thred is completed and next thread is executed,her we don’t give any time.
    where as sleep is , if u give some time. here one problem occur even the first thread is completed there process.next thread is execute,but before execute it will check time pried

  6. Divya says:

    Why it is named as java as java language?

    • Anil says:

      It is technology.

    • suv says:

      James Gosling came up with the name ‘Oak’ (he created this language for creating a Smart refrigerator (he hated C, so created his own!!)) while working for IBM I guess. but then the lawyers didnt agree for copyright issues as there was another language with that name. then they (Gosling’s team) came up with the name Silk, which Gosling didnt like (sounded too Gay) then they again came up with a name ‘Lyric’ which again the lawyers flushed.

      So finally they just came up with a bunch of names which one or the other agreed to and the lawyers didnt object to.. Java was one of the names (as they were working in Sumatra (earlier called java) (dJakarta)).. then they fired up a random number function written in *java* itself. and the name Java was chosen..

      ** its not really a stupid question, of course not a scientific one, but definitely not stupid. of course they could have ended up with a name Bava had there been a Telugu in the team (no offence!) :P.

  7. Saravanakumar says:

    Hi Prasanth,

    Object.wait() does release the monitor ownership. But Thread.sleep() does not release the monitor ownership. which means thread.sleep() is resource intensive where object.wait() is not.

  8. Shailendra Kumar Chauhan says:


    Your way of explaination is excellent.


  9. Vishal says:

    @ Divya ., Answer:- Bava ani pedithe bagundadhu kaabatti., in english., it is not fare if we called it as BAVA (brother in law) .. Dont ask silly questions., useless fellow

  10. ashis kumar mohanty says:

    can u explain what is resource intensive ?
    e.g. thread.sleep() is resource intensive.
    plz explain..

  11. Ramesh says:

    This is really helpful, Better if you post some articles on springs and Web Services also.

  12. Anonymous says:

    why wait(),notify(),notifyAll() are declared in java.lang.Object instead of java.lang.Thread

  13. sam_a says:

    Good article Joe i liked it very much

  14. Dharmesh says:

    Pls give some knowledge about Struts

  15. Satish says:

    Very Good Explanation

  16. vinod kotha says:

    really superb blog. hats off!!!

  17. Rupa says:

    thanks joe

  18. Anil says:

    Your website Designing and logo presence and exposure is too much impressive………..Article so….

  19. abhiashek yadav says:


    i am abhishek yadav,
    pls explain the life cycle of servlet with pic $ also a demo for it….

  20. ankit says:

    Really Good explanation …
    Not only this but all topic you explain so far ….
    Like your skill .

  21. ankit says:

    Really Good explanation …
    Not only this but all topic you have explained so far.
    Like your skill .

  22. Tristan says:

    This site is really helpful.
    Thanks Joe.
    I have one question–
    Can we call init(config) method from destroy() method?

  23. Siva says:

    Hi Joe,
    When ill the destroy() method called by default.

  24. Devanshi says:

    @ Joe
    really helpful website sir but i found a contradictory statements….
    *here ur saying dat destroy wont effect but in servlet life cycle concept u said dat ” if destroy() method is invoked first, then Servlet is removed from the container and then eventually garbage collected.”
    can u pls explain me?
    thnx in advance…

  25. mahesh says:

    Hi Joe,
    I have a question , from where and when service() method is getting called ? Is it before loading Struts-config.xml ?

  26. lobby says:


  27. Raghu says:


    I have developed a web application, when user enter the input and start it communicates to the servlet through Ajax call and executing and there is a option to stop the Ajax call.Ajax abort function terminates the connection, but still the servlet is thread executing.

    How can i stop the servlet(thread) executing?

    The reason why should i stop servlet is to avoid collapse after the second request.

  28. Rahul says:

    How to kill a servlet manually/forcefully. I was asked this question in interview

Your Comment

↑ Go to top