• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

When is destroy of servlets exaclty called

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
sucheta shanbhag
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
https://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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it so that sessions are maintained even after the container restarts? I mean the session object...forget cookies in this scenario.
 
Amit Goyal
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic