GeeCON Prague 2014*
The moose likes Servlets and the fly likes How is Servlet a singleton? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How is Servlet a singleton?" Watch "How is Servlet a singleton?" New topic
Author

How is Servlet a singleton?

Akhilesh Murthy
Greenhorn

Joined: Oct 07, 2009
Posts: 24

Hi,

Servlets are singleton, but how are they made as singleton? How can any class be made Singleton?

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Servlets are not singletons.
Akhilesh Murthy
Greenhorn

Joined: Oct 07, 2009
Posts: 24

Could you please elaborate why Servlet is not considered as Singleton?. Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

The servlet specification says that a servlet container can create more than one instance of a servlet if it decides it's necessary to do that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

It's really the other way around... You'd need to prove that it is a singleton rather than whether it's not.

Just because a container will customarily only create a single instance of a servlet does not make it a singleton. You can create one instance of any class -- that's not what a singleton is. A singleton is a class that cannot have more than one instance.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Akhilesh Murthy wrote:How can any class be made Singleton?


And of course, since Singletons Considered Harmful, you don't want to make any class into a Singleton anyway.
Akhilesh Murthy
Greenhorn

Joined: Oct 07, 2009
Posts: 24

Pat Farrell wrote:
Akhilesh Murthy wrote:How can any class be made Singleton?


And of course, since Singletons Considered Harmful, you don't want to make any class into a Singleton anyway.


Why is Singleton classes considered harmful? and would there be any scenario where i would need to use these classes?

If so then how would i create one?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Akhilesh Murthy wrote:Why is Singleton classes considered harmful? and would there be any scenario where i would need to use these classes?
If so then how would i create one?


Google for "singletons considered stupid" there is a lot of information.

Since they are stupid/harmful, you never need them. So I won't tell you how to create one of the stupid things.

Make sure you find the link to https://sites.google.com/site/steveyegge2/singleton-considered-stupid

Here is just a few of the major problems with the stupid things.


++One is memory management; a Singleton is basically just a memory leak,

++Speaking of memory leaks, what if your Singleton has a handle to some limited resource, like a database or file handle? I guess you get to keep that sucker open until your program ends.

++Another issue is that the Singleton design is syntactically noisy; you have to stick in boilerplate code not only in the Singleton, but in everyone who uses it.

++ static methods are as flexible as granite. Every time you use one, you're casting part of your program in concrete.

++ it makes unit testing impossible

++ it grossly increases coupling and decreases cohesion. Good engineering wants low coupling and strong cohesion.
Akhilesh Murthy
Greenhorn

Joined: Oct 07, 2009
Posts: 24

Thank you.
Miku Ranjan
Ranch Hand

Joined: Oct 11, 2011
Posts: 98
Hi,
For using singleton object I best experienced with spring framework.
 
GeeCON Prague 2014
 
subject: How is Servlet a singleton?