• 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

Singleton or servlet context attribute

 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need an instance of some object serviced all requests. Is it reliable to put it in context instead of defining singleton?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on your need and the context in which you are using. It seems you can put your that object with Context.For each WebApp your are having only One ServleContext.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by D Rog:
I need an instance of some object serviced all requests. Is it reliable to put it in context instead of defining singleton?



How is it more reliable to put in the context?
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ofcourse by putting it as a context attribute, it would make your life earier in jsps if you require to access the object properties especially so if you are using technologies like jstl and el which provides transparent scope lookup.
Iam not sure about the singleton even otherwise though.

Where are you accessing it ? In your servlet or jsp's service methods ? Is the singleton object obtained as a local reference in these cases ? If so there is an even chance that it may be gc-ed. (somebody correct me if you know this for wrong).

Application context attributes are specifically used in such cases and I think, may be the best practise to adopt.

cheers,
ram.
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If my singleton stored as an attribute keeps state and somebody removed accidentally or replaced by another copy? It's my concern only. Singleton is quite self protecting, so it isn't easy to destroy or recreate it. That I meant by more reliable. Anyway, attribute looks better for me, so thank all for your input.
reply
    Bookmark Topic Watch Topic
  • New Topic