Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General 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
  • 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: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
  • 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: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic