This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Application Scope or Static Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Application Scope or Static Methods" Watch "Application Scope or Static Methods" New topic
Author

Application Scope or Static Methods

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61086
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

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.


Java Experience
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

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.

SCJP 91%, SCWCD 97%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Application Scope or Static Methods