Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Application scoped object best practice

 
Alan Yap
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alan Yap
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic