Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Application Scope or Static Methods  RSS feed

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to store a Java object into an application scope. I am to decide
between two possible implementations viz.:

1. use a class with some static properties and some static accessor methods
which can be accessed by any JSP/servlet thread. The shared (application )
object will be there in the static property and will be accessed by the
static methods.

2. use ServletContext's getAttribute() and setAttribute() methods.Store the values in Application Scope

Which is better
 
Author and ninkuma
Marshal
Posts: 66799
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Storing a value in application scope makes it truly global to that application and just that application. It avoids relying on classloader details.

I almost always store app-wide data in application scope.
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More info about the variable would have helped.

Anyways, I will not write a class with one static member. Do you have a class in mind for storing this value.

Both are not threadsafe.
 
Ranch Hand
Posts: 88
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I faced similar issue and here's what I concluded:

1. This should be done only for readonly data. If values are modifiable the they should be read from persistent store always.
2. If you need to cache some data (say from xml files or database) and it is availble at application startup (which is most probable), use servlet context.
And to load the data only use context listener.
3. If data is not availble upon server startup then use static members.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!