File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Controller servlet better practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Controller servlet better practice" Watch "Controller servlet better practice" New topic
Author

Controller servlet better practice

Servin Park
Greenhorn

Joined: May 10, 2002
Posts: 26
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

Joined: Mar 09, 2001
Posts: 2676
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?


Matthew Phillips
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Controller servlet better practice