aspose file tools*
The moose likes Servlets and the fly likes again singleton and servlet class loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "again singleton and servlet class loading" Watch "again singleton and servlet class loading" New topic
Author

again singleton and servlet class loading

Luca Cazzaniga
Greenhorn

Joined: Feb 16, 2013
Posts: 12
Hi all,

searching in forum I found a previous post on this subect, and i don't get the concept yet, I'm afraid to ask further clarification..

http://www.coderanch.com/t/484632/Servlets/java/Singleton-servlet

Few concept but confused:

Every servlet is loaded the first time is used and remains in memory until the container is reloaded/restarted but what about the availability of the class used by it?

is correct states that using a static call (the instantiation method of the singleton) in a servlet/listener/filter yields the storing of the class until the container is reloaded?

so if more entities call the instantiation method all gets the same object?

could anyone advise me for a ioc pattern tutorial not based on framework prerequisite, a stupid example or template to implement the pattern would be great..

Best regards

Luca
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
It is up to the container to handle creation and initiation of a servlet instance - the servlet contract says that this will all happen before the first request is handed to the servlet instance.

The following statement:
so if more entities call the instantiation method all gets the same object?


is meaningless because there is only one entity - the container - that will ever call the "instatiation" method and it only does it once.

Bill
Luca Cazzaniga
Greenhorn

Joined: Feb 16, 2013
Posts: 12
Hi William thanks for your time,

that sentence is deceptive. what I means is if there were more servlet/filter/listener using the same singleton...


anyway the loading of the Singleton class happens at the first call in whatever servlet/filter/listener call it.
the unloading of the Singleton class happens at container reloading, and it is becouse the servlet/filter/listener uses it and remains in vm memory until the container restart?

So different thread always refers to the same singleton instance. Could you confirm?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

I'm not sure what your question is. Are you asking if singletons work differently in a servlet container than in other places, or what?
Luca Cazzaniga
Greenhorn

Joined: Feb 16, 2013
Posts: 12
Yes I was... I got it now.

better later than never

Regards

Luca
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Good, glad you got it straightened out. I find it amazing how people seem to be blinded by servlets and think that Java somehow works differently there -- you aren't the only one.
 
wood burning stoves
 
subject: again singleton and servlet class loading