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.

previous post: Java Final Keyword

next post: Java Marker Interface

31 comments on “What happens if you call destroy() from init() in java servlet?

  1. 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?

    • 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. Very nice documentation. My request to u is to Add Struts documentation also.

  3. 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.

    Thanks
    Niraj

  4. HI PARASNTH
    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

  5. Why it is named as java as java language?

    • 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.

  6. 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.

  7. Hi,

    Your way of explaination is excellent.

    Thanks

  8. Pingback: Java Pins

  9. @ 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. can u explain what is resource intensive ?
    e.g. thread.sleep() is resource intensive.
    plz explain..

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

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

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

  14. hi..

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

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

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

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

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

  19. @ 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…

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

  21. Hi,

    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.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>