Can I call destroy() method inside the init() method?

Discussion in 'Servlets & Java Server Pages (JSP)' started by Nalan, Dec 3, 2009.

  1. Nalan

    Nalan New Member

    Give me some example code....


    Thanks In advance
    nalan
     
    Last edited: Dec 3, 2009

  2. khadangap

    khadangap New Member

    HI Friend,
    I have collected the answers of this Q and hope it will help you
    What will happen if we call destroy() method from inside the


    Whatever code is in the destroy method will be executed as soon as the servlet is loaded.init() method?


    So will the servlet be destroyed and not initialized?
    I was asked this question in an interview.

    No. As Ben said, whatever code you put in the destroy() method will be called. That's all.

    So will the servlet be destroyed and not initialized?
    I was asked this question in an interview.


    I've been wondering why that question gets asked so much.
    I guess it would make a good interview question.
    If that's the answer you gave, I'm assuming the interview didn't go very well.

    There is a link to the servlet spec in my signature.
    You might want to download it and read the section that covers the servlet life cycle. It's not a difficult read.

    All of the life cycle methods init, destroy, doXxx, etc.. are callback methods. Instead of the spec providing these methods for you to call, you're supposed to implement the functionality of these methods for the container to call.

    For instance, if you want something done when the servlet is initialized, you would override one of the init methods and add the code that you want executed in there. Likewise, if you want something to happen when the servlet is removed from service, you would add your code to the destroy method. If you don't override destroy, then nothing happens when destroy is called. You would never call destroy yourself. It is called by the container when the servlet is taken out of service.

    So, to answer your question:
    If you don't override destroy(), but added a call to it from the init method the container (when the servlet is put into service), will call your init method.
    This will cause the default destroy method (which does nothing) to be called. Nothing will happen.
    Then, when the servlet is taken out of service, the container will call the default destroy method. Again, nothing will happen.

    If you override destroy then, when the servlet is put into service, the container will call the init method which (because you're calling destroy from init) will call the destroy method. When this happens, whatever code you put into destroy will be executed. Then, when the servlet is taken out of service, the container will call destroy and your code will be executed again.

    Is their a way to explicit destroy the servlet from service method. As per discussion above, it seems if destroy() method is called from service or init method, it will just execute the code inside that method.But what if i really like to want to destroy the servlet instance based on some condition from service method.

    Probably the safest and most universal way to take the servlet offline is to have one of your service methods throw an UnavailableException. This will ensure that it will not get any more requests and let the container gracefully handle any existing requests.


    Your reply was
    "If you don't override destroy(), but added a call to it from the init method the container (when the servlet is put into service), will call your init method.
    This will cause the default destroy method (which does nothing) to be called. Nothing will happen.
    Then, when the servlet is taken out of service, the container will call the default destroy method. Again, nothing will happen."

    The statement in bold specifies that nothing will happen when the destroy method is called from init method.
    The statement that has underlined specifies that nothing will happen when the container calls the destroy method.

    So,does that mean that the servlet willl never be destroyed!!!???
    I am totally confused. Please help me out.

    You left out a crucial word when you quoted the underlined text: "default". The default destroy method does nothing; if it is overridden, then whatever code the overriding method contains will execute. No, the servlet will be destroyed after the container calls the destroy method. It's just that no user-specified action will happen, because there wasn't any.

    Now please tell me if my understanding is correct. I have listed down my understanding here below.
    1. If default destroy method is called from init method, nothing happens. The init process continues
    2. If default destroy method is called from service method, nothing happens. The service process continues
    3. Once step1/step2 is over and after servlets job is over, the container will call the destroy method and the servlet wil be removed from the container. This will happen once the servlet determines that the servlet is no more in use.
    4. If destroy method is overridden and destroy is called from init, init process will continue even after executing the overridden destroy method - (In this case, is it possible to to override the destroy method so that the servlet is removed from the container?)
    5. Once step 4 completes, remaining action is same as step 3
    No. If your code calls the destroy method, then the servlet will not be taken out of service. That's what Ben meant by "Whatever code is in the destroy method will be executed as soon as the servlet is loaded." in his first post. If your code calls a method, then that method will be executed, and that's it. In particular, if your code calls the destroy method then the servlet will not be taken out of service.
    Again - overriding or calling the destroy method does not lead to the servlet being taken out of service. The container does that when it sees fit to do so (and after it has called the destroy method). The servlet code can't cause the servlet to be taken out of service, unless it does what William mentioned.

    I would simply take it as:
    1. init() : A method that is executed as soon as the Servlet is alive.
    2. doXXX() : A servlet is living and performing.
    3. destroy() : A method that is executed just before the servlet dies.

    This is better than confusing functionalities with names.
     
  3. chandrakanth.amuru

    chandrakanth.amuru New Member

    hi,
    yes we can call the destroy() from init() but it's not recommended to call the destroy(). B'cause "typically" the code present in destoy() makes the variable servletConfig as unreferenced hence we can't use it in further code.
     
  4. chandrasekhardutta

    chandrasekhardutta New Member

    hello boss

    ca u please tell me from which init() do u want to call destroy() ?
    there r two init methods available according to specification.

    they are (1) first init()
    (2) second init()

    first init() will be executed by the web server. web server is not responsible to execute the second init(). first init() is responsible to call the second init().
    So u can write destroy() as part of second init().

    if any related queries u can send me on chandrasekhardutta@gmail.com

    regards
    shekar
     
  5. dkjena4u

    dkjena4u New Member

    No we cannot call destroy() from init(). the serlet class will not compile. It will generate compile time error.
     

Share This Page