This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

again singleton and servlet class loading

 
Luca Cazzaniga
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I was... I got it now.

better later than never

Regards

Luca
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic