This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet container will create only one instance of the class?

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, On what basis Servlet container will create only one instance of the class.I read that Servlet is not a Singleton class.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class doesn't have to be a singleton to have only one instance. Anyone can just choose to create a single instance of any class.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we create single instance besides Singleton
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Specifically, you don't create servlet instances. The container handles all that.

More generally, you can create a single instance by only creating one instance. For example:There! I've created one instance of a bird. If I don't ever create another, I'll have only one instance. And yet, Bird is not a singleton.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe internally the design would be :

a "Map" with key as "Servlet Class" name and Value as the "instance of the servlet class"...

If the servlet is first time loaded, container searches the map with the servlet class name as a key...

if it does not find, creates a new instance and makes an entry in the Map...


If Servlet class name exists in the Map, it will get the Object binded to the key and uses the same object...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic