• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How is Servlet a singleton?

 
Ranch Hand
Posts: 31
1
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Thanks
 
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlets are not singletons.
 
Akhilesh Murthy
Ranch Hand
Posts: 31
1
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please elaborate why Servlet is not considered as Singleton?. Thanks.
 
Paul Clapham
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 31
1
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 31
1
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
For using singleton object I best experienced with spring framework.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic