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

27/05/2008

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.

Ads by Google

20 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?

  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?

  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. Ads by Google
  10. @ 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

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

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

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

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

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>

ABOUT
I am Joe, author of this blog. I run javapapers with loads of passion. If you are into java, you may find lot of interesting things around.
Ads by Google
STAY in TOUCH:

Email:

Core Java | Servlet | JSP | Design Patterns | Android | Spring | Web Service | © 2008-2012 javapapers.