aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes When is destroy of servlets exaclty called Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "When is destroy of servlets exaclty called" Watch "When is destroy of servlets exaclty called" New topic
Author

When is destroy of servlets exaclty called

sucheta shanbhag
Greenhorn

Joined: Sep 28, 2007
Posts: 18
HI all,

Today is my first day in the javaranch community.I am planning to take up the exam in a months time.
I have a questions on servlets destroy() method.

When exaclty is the servlets destroy() called....

One is wen the servlet is out of service...
and is it also called wen the conatiner is shutdown???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41879
    
  63
Welcome to JavaRanch.

Whenever the web app is stopped, or the container is shut down, the servlets are taken out of service, so the destroy method would be called.


Ping & DNS - my free Android networking tools app
sucheta shanbhag
Greenhorn

Joined: Sep 28, 2007
Posts: 18
Thanks for the reply....

One moredoubt...
are sessions mainained even after container restsrt in some cases???

and when the client closes the browser the sessiosn is lost right??
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
The destroy() and init() methods are called only ONCE in a servlet's lifetime while the service() method may be called multiple times.

The init() method is called after the container loads the servlet class and instantiates and then wants to initialize. It is never called again.

Similarly, the destroy() method is called only once, when the servlet instance is going to be destroyed and never again ( the servlet is history, how could it be called again? ) Now, when the servlet is going to be unloaded depends on the container. It will definitely be unloaded if it is still present when the container shuts down or the application shuts down.

But, it may also be unloaded if the container decides that there is a shortage of memory and that this particular servlet hasn't got a request in a long time and so, is a prime candidate to be removed.

In either case, destroy() will be called ( only this one time ) by the container to tell the servlet that it's going to be unloaded and so it should perform whatever clean up it needs to and prepare to be destroyed.

EDIT: Ok, this is really strange and frustrating and makes me look bad ( ). When I start replying to a post, like this one, I see no replies. But when I finish and reply, earlier replies suddenly appear and judging by the timestamps they don't seem like they were posted in the time I replied, because I don't type that slow!
[ September 29, 2007: Message edited by: Tarun Yadav ]
Amit Goyal
Ranch Hand

Joined: Feb 21, 2006
Posts: 95

One moredoubt...
are sessions mainained even after container restsrt in some cases???

Yes, sessions are maintained even after container restarts.



and when the client closes the browser the sessiosn is lost right??


No, the session is not lost on the server, it is there and will be lost on session-timeout.


- Amit Goyal
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
Originally posted by Amit Goyal:


Yes, sessions are maintained even after container restarts.



Is this behaviour is required by the specification? Or is it a vendor specific feature? Because I ( now ) know that Tomcat does this but what about other containers?

What I read right now:
http://www.coderanch.com/t/87114/Tomcat/Load-objects-session-upon-restart
http://forum.java.sun.com/thread.jspa?threadID=575302&messageID=2870473
http://tomcat.apache.org/tomcat-5.5-doc/cluster-howto.html
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
Did a little bit more looking around, this seems to explain it:
http://tomcat.apache.org/tomcat-5.5-doc/config/manager.html

Still going through it though
hatim osman
Ranch Hand

Joined: Jun 10, 2005
Posts: 105
Hi
Amit , do you mean to say that sessions sruvival across shutdowns and restarts is mandatory by the specification or it is vendor specific?
In case of tomcat, does a call to a session getter method returns a new sessoon or fetch that "maintained by the container" if the applicaition crashes after acquiring a session and then restarts?

I still find it confusing!
Hatim
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9384
    
    2

Is it so that sessions are maintained even after the container restarts? I mean the session object...forget cookies in this scenario.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Amit Goyal
Ranch Hand

Joined: Feb 21, 2006
Posts: 95
You get the old session only when you have configured some sort of persistance storage (e.g. file system, database etc.) for sessions (which is generally configured in the container's conf file) and have made always save session element to true. In case of resin server it is specified in resin.conf.

If you do above configuration then you will get the old session on the container restart/crash.

Well, these configuration are handled in the vendor specific ways.


-Amit Goyal
[ October 01, 2007: Message edited by: Amit Goyal ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When is destroy of servlets exaclty called