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

How is Servlet a singleton?

 
Akhilesh Murthy
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are not singletons.
 
Akhilesh Murthy
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please elaborate why Servlet is not considered as Singleton?. Thanks.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64828
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Miku Ranjan
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
For using singleton object I best experienced with spring framework.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic