• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interview question: can you make a servlet as singleton?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you make a servlet as singleton.. this is a question asked in an interview..

According to my knowledge , as the servlet object is created by the container , we can't create it.. as it calls the default constructor while creating servlet.

The only method i know to make an object singleton is 1) making its constructor private and 2)giving factory methods which gives objects ... (which returns only one object .. and returns the same on further calls..)


But the first point , making constructor private is not possible when servlet calls the constructor..

.................

But I have a doubt, the Action Servlet in Struts framework is singleton .. that means there is way to create a singleton servlet .. ...

So .. tell me if there is any way.. and correct if Iam wrong in any of the things...
 
Marshal
Posts: 28193
95
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 always singletons according to the latest servlet spec as long as you write them normally. (Which means not implementing SingleThreadModel.)
 
Prashanth Reddy Padamati
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul Clapham : "Servlets are always singletons according to the latest servlet spec as long as you write them normally. (Which means not implementing SingleThreadModel.) "

Servlet are by default single instance multi threaded. But not singleton.. I don't know if there is any changes in the latest versions.. please correct me.. and thanks for reply
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prashanth Reddy Padamati wrote:please correct me..


Singleton is a design pattern - which give you only one instance of a class . so as paul said container creates only one instance to a servlet unless you implements deprecated SingleThreadModel, even there are multiple request/thread.
 
Prashanth Reddy Padamati
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy : "Singleton is a design pattern - which give you only one instance of a class"

But I studied somewhere that "The WebLogic server creates more than one instance of a server when there are more requests demanding servlet. Due to this only, ActionServlet is explicitly made Singleton by the Struts Framework designers.... "

Any body clear this point..... whether servlet can be made as singleton or not..(i.e. we control the creation of instance )

Thanks for reply Seetharaman Venkatasamy
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic