aspose file tools*
The moose likes Servlets and the fly likes destroy() method call in sevlet's life cycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "destroy() method call in sevlet Watch "destroy() method call in sevlet New topic
Author

destroy() method call in sevlet's life cycle

ashis behera
Greenhorn

Joined: Nov 19, 2006
Posts: 7
destroy() should be called by the container or we should override it ?

if destroy() called and after that client makes a request how can the client get the response as there is no db connection?

so when should we call the destroy() method (when we would know that there would be no further request from the client? )

asked in an interview

please help!!!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

This is what the Servlet API says,

It is for the Servlet's destroy() method


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.



One good suggestion is NOT to override this life-cycle method.
[ January 01, 2008: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

Originally posted by Raghavan Muthu:
One good suggestion is NOT to override this life-cycle method.

Why not?

The method exists to be overridden when the servlet needs to do something upon being taken out of service.

What makes no sense is for your code to call this method; that's the container's job. Overriding it is a perfectly fine thing to do.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Ouch..

Thank you Bear. That was very true.

We should NOT call this method by our own.
Carlos Cunha
Greenhorn

Joined: Dec 13, 2007
Posts: 3
I think the following paragraph answers your overriding question:
"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."

As for the calling destroy() question, since destroy() is a life-cycle method you should let the container take care of it and not call by yourself.


SCJP 5.0<br />SCWCD Ed. 5(Preparing)
ashis behera
Greenhorn

Joined: Nov 19, 2006
Posts: 7
thank you guys for the response, but my question is a little different.

the interviewer argued that we should override that method, we should not depend on container to do the job.so when should we call that?

when would we know that client would not make a request, since if client makes a request then it would not get the response because there is no db & oter resources available.

please help.
Carlos Cunha
Greenhorn

Joined: Dec 13, 2007
Posts: 3
Originally posted by ashis behera:
...
the interviewer argued that we should override that method, we should not depend on container to do the job.so when should we call that?
...


hi ashis,

here is the thing, if one of the container's job is to take care of a servlet's life-cycle, why should we not depend on it? Can you see my point?

As we can see, we all understand and accept the destroy() method overriding, specially because every application has its own set of resources that must be taken care.

But, why not depend or thrust your container to do its job? If that is the problem, change the container

Hopefully I helped
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Obviously there are two entities which could take up the job of invoking the method. Either the container OR the developer (you).

You know the pros & cons of each.

It is something like the difference between Bean Managed Persistence (BMP) and Container Managed Persistence (CMP) of Entity Beans.

If at all you are supposed to invoke the lifecycle method, literally you will end up in reinventing the wheel as by writing to code for the job of the container which is already existing!
[ January 02, 2008: Message edited by: Raghavan Muthu ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Originally posted by ashis behera:
the interviewer argued that we should override that method, we should not depend on container to do the job.


I think you (or the interviewer) is confused about the concepts of overriding and calling that method. Your code overrides the destroy method, while the servlet container calls it. It's the same way the init method work (as well as doGet, doPost, etc).


Ping & DNS - updated with new look and Ping home screen widget
ashis behera
Greenhorn

Joined: Nov 19, 2006
Posts: 7
thanks again for the response.

with reference to your answers i am posting something.please look to this carefully....

all are saying that we should let the container do it's job say life cycle methods.

but do we know at what point of time it would call them? (specially destroy())
i know that after service() when the response is given it calls.

but my question is if there is another request from the client after destroy() method is called then what would happen???

we don't know when the client wouldn't make request.

so from my point of view the container should call destroy() when it is sure that there would be no further request from the client.

so how would container know that there would be no further request from the client???

please get my point guys...please..........
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
but do we know at what point of time it would call them? (specially destroy())
From the Servlet.destroy javadocs: Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. In other words, whenever the servlet container thinks that a servlet should be taken out of service. One such occasion is server shutdown, another might be a reload of a web app.

i know that after service() when the response is given it calls.
Not sure what you mean by this. Calling service does not automatically cause destroy to be called afterwards.

if there is another request from the client after destroy() method is called then what would happen?
The client would receive an error message, possibly a 404.

so from my point of view the container should call destroy() when it is sure that there would be no further request from the client.

The container can't know that, and doesn't (and shouldn't) care either.

Obviously, taking a servlet out of service when it should be in service is not a good idea, so unless the web app or the server is being shut down, the server had better put another instance of the servlet in service if it takes one out of service.

The bottom line is that the server may decide to take servlets out of service without your code being able to do anything about it. So don't write code that relies on the init method of a servlet class being called exactly once during the uptime of a web app - it may be called several times, with calls to the destroy method in between.
 
 
subject: destroy() method call in sevlet's life cycle
 
Similar Threads
Doubt in servlet
what happend,when we call destroy() in service() method?
when and how all methods of servlet are called
overloading of init-service and destroy methods
destroy() method