How to call destroy() in servlet from eclipse?
Asked Answered
P

4

5

When I shutdown server, destroy() is not called in Eclipse.

public class Demo extends GenericServlet {

    public void init(ServletConfig config) throws ServletException{
        System.out.println("intit intialized");
    }

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        System.out.println("servicccceeeeeeeee method........");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.print("<h1>service method</h1>");
        out.close();
    }

    public void destroy() {
        System.out.printlnln(".........destroy method invoked.......");
    }

}

When and how do I call the destroy method?

Potent answered 6/8, 2015 at 8:25 Comment(1)
since OP not clear about the problem. giving one down voteDeidredeific
C
9

It won't be invoked when you abruptly terminate the whole Java Virtual Machine altogether. I.e. when you press the red square button in Eclipse's Console tab.

enter image description here


It will be invoked when you gently stop or restart the server itself. I.e. when you press the red square button in Eclipse's Servers tab.

enter image description here

Chromate answered 6/8, 2015 at 13:2 Comment(1)
man, you saved my day. I was stuck for nearly 1 hour. Thank you very muchLewandowski
B
2

In Eclipse, destroy() is called only if you gracefully shut down your application. If you kill it with the stop button, or if you unplug power supply to your computer destroy() will not be called.


And now more about the method itself:

Servlet.destroy()'s javadoc says:

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.

This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet. This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.

It doesn't specify what situations would cause a servlet to be "taken out of service", it is simply an event to which you can react to if you need to. So in destroy you should clean up your Servlet if there is anything to clean up, you could store the state of the Servet, and perhaps log the error. It could happen, for example, because Server was out of memory.

Boggess answered 6/8, 2015 at 8:33 Comment(5)
The destory() will be called : when the container shuts down or the application shuts down; when the container decides that there is a shortage of memory; when this servlet hasn't got a request in a long time. why in my program not called destroy() in eclipse.......can you tell me with example by using this code...Potent
Nobody says that destroy() is called when server is shut down. Why do you think that this method should be called in that case? It's called in case some thread fails, some error happens, this servlet becomes unavailable...Boggess
somebody says ...destroy() is called when server is shut down..But it is not possible in this program.. thank you for repliedPotent
You're not answering the concrete question about the destroy() method not being called in Eclipse.Chromate
Thanks @BalusC. I fixed it.Boggess
D
1

The destroy() method is called by container before removing a servlet instance from service and gives servlet an opportunity to clean up any resources being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.

The destroy() and init() methods are called only once in a servlet's lifetime while the service() method may be called multiple times. The destory() will be called :

1.when the container shuts down or the application shuts down;

2.when the container decides that there is a shortage of memory;

3.when this servlet hasn't got a request in a long time.

After the servlet container calls this method, it will not call the service method again on this servlet.

Deidredeific answered 6/8, 2015 at 13:16 Comment(3)
It's working fine in eclipse..thank you for valuable answered.Potent
you may accept the answer if it has resolved your problem :) :)@anandkumarDeidredeific
could you please give me the reason for not accepting the answer after accepting it. @anandkumarDeidredeific
M
0

destroy method calls automatically when you stop the server which is running in your application. In my application where i am using tomcat so when i stops the server then automatically calls the destroy method.

enter image description here

Marketing answered 31/1, 2020 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.