Is it possible to call destroy () method from init () and service () methods in Servlet?

Is it possible to call a method destroy()from methods init()and service()in a servlet? I have many confusing answers on blogs.

As I understand it, when we call a method destroy()from init(), it must call and destroy the servlet if we redefine it destroy()in our servlet. Then the servlet will be destroyed.

Is this understanding correct?

+5
source share
1 answer

Not everything is right.

destroy() , . . . destroy() . , , init().

.

private SomeExternalResource someExternalResource;

@Override 
public void init() {
    someExternalResource = new SomeExternalResource();
}

@Override
public void destroy() {
    someExternalResource.close();
}

, .

+11

All Articles