*
The moose likes Servlets and the fly likes Servlet container will create only one instance of the class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet container will create only one instance of the class?" Watch "Servlet container will create only one instance of the class?" New topic
Author

Servlet container will create only one instance of the class?

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, On what basis Servlet container will create only one instance of the class.I read that Servlet is not a Singleton class.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
How can we create single instance besides Singleton
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

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

Joined: Jul 11, 2010
Posts: 247

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...

SCJP 6 [SCJP - Old is Gold]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet container will create only one instance of the class?