wood burning stoves 2.0*
The moose likes Servlets and the fly likes Instance variables in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Instance variables in Servlet" Watch "Instance variables in Servlet" New topic
Author

Instance variables in Servlet

Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Hi All,
Recently I read an interesting article on a particular Servlet design pattern. In this pattern, FrontServlet evaluates a request, and hands off the request to the appropriate RequestHandler (a common design as you probably know). Now, FrontServlet has an instance variable--a HashTable of registered RequestHandler objects. Because these RequestHandler objects are instance variables, will we run into concurrency issues?

For example, if multiple "login" requests come in, FrontServlet will hand off the request to its LoginHandler instance. The LoginHandler class itself does not have any instance variables, so I don't believe there will be concurrency issues, but I want to be sure this is a good design.

What do you guys think about this?

If you're interested, the article is here.
[ October 26, 2004: Message edited by: Jeffrey Hunter ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

FrontServlet has an instance variable--a HashTable of registered RequestHandler objects. Because these RequestHandler objects are instance variables, will we run into concurrency issues?


Only if the servlet code outside of init modifies the hash table (why not HashMap?). If the hash table is set up in init and then is only read in the service code, there should be no contention issues.

I'd still say that it's a pattern to be avoided. I'd rather see the servlet store the hash in application context and reference it from there.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Thanks, Bear. Sorry so long to reply, but I've been swamped with a new project. <i>Hashtable</i>, yes, it was an old article I suppose. Anyhow, I've declared a synchronized HashMap as an instance variable, as its being used by the Dispatcher class exclusively. We'll see how it goes!
 
Don't get me started about those stupid light bulbs.
 
subject: Instance variables in Servlet
 
Similar Threads
Factory Pattern and instanceof
accessing clicked hyperlink value of jsp page in servlet
doubt abt SingletonPattern
Extending Servlet , Caching in init()
Urgent !!! Scalabilty of Front Servlet