File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Controller servlet better practice

 
Servin Park
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic