This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Application scoped object best practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Application scoped object best practice" Watch "Application scoped object best practice" New topic
Author

Application scoped object best practice

Alan Yap
Ranch Hand

Joined: Feb 24, 2006
Posts: 32
I have this servlet (names changed to protect the innocent) that caches a bunch of data for my web app:




This servlet will be made an applicate object in my jsp pages via:

<jsp:useBean id="MYSTUFF" scope="application" class="example.mystuff" />

Most of my jsp pages will get the cached data from this object. I am just wondering, will those jsp pages (in visitors' machine viewing my website) be affected fatally* when I (as an admin) performed a reload() call to update the hashtable inside this object/servlet?

*fatal as in causing a servlet exception during the reloading process, perhaps?

If there are possible risk of exception, what is the best practice to handle such methods of caching?
[ December 05, 2007: Message edited by: Alan Yap ]

When I'm not coding.. I do THIS
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You don't want to try to bind the servlet itself to application scope.
In fact that won't work. You'll end up with two instances of your class, one created by the container and a second created by the useBean tag.

If all you want to do is make a HashMap available, you can simply bind it to application scope. You can do his from a servlet or from a contextListener when the application starts.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Alan Yap
Ranch Hand

Joined: Feb 24, 2006
Posts: 32
From your reply, does this means if the hashtable is storing 1kb of data, my method above would consume twice the memory size? (as 2 instances are created). I have no problem reloading the hashtable but I'm scared during high traffic volume of my site, performing a reload to my application scoped data will caused the jsp page failed to be rendered at the same time.

Would an application scoped Hashtable be equally effective as a hashmap? Hence..



Well besides having some data be stored inside the hashtable (inside my application servlet), I will be storing another set of data inside some arrays too (with size up to 200 items per array).
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

My point is that a servlet is an object that listens for web requests and responds to them. It is not a datastructure that you would bind to scope.

If you need your hashtable to be available to all objects in the application then bind it to context scope. Don't make it an instance variable in a servlet and certainly, don't try to bind a servlet to any of the scoped objects.
Alan Yap
Ranch Hand

Joined: Feb 24, 2006
Posts: 32
thanks! things are getting clearer now.

I found out that I've been using the wrong "implementation" in my codes



Any comments if I made it a serializable object instead?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Application scoped object best practice
 
Similar Threads
Appending row to end of JTable
what is the best way to separate layers in data structure
how to load on startup and get an instance of class?
problem using generics jdk1.5 in jsp
When to use javabeans or servlet