• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Controller servlet better practice

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I am building a webapp with MVC pattern. In my controller servlet, I need to register eventHandler classes for later use. Following are two options that I can think of:
1. Declare "private HashMap eventHandler" class level variable and in the init(...) method initialize the eventHandler variable with every event handler classes using reflection eventHandler.put(key, ((EventHandler) Class.forName(value).newInstance())). And get appropriate event handlers depending on request parameters.
2. Set up a method that returns appropriate event handler in the controller servlet using switch():
...
A major difference I can think of between those two options is number of times Class.forName(..) method is called. First option calls the Class.forName(..) method once for every eventHandler for the life of the servlet. Second option call the Class.forName(..) method every time there is request. Which one of those options is "better practice"? Is there any obvious flaw in logic? Is there "better practice" that those options? Many thanks in advance for your input.
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like option number one, but why build the MVC yourself when it has already been done for you? Have you looked into using Struts?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic