Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance variables in Servlet

 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65228
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic