File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Controller servlet better practice" Watch "Controller servlet better practice" New topic

Controller servlet better practice

Servin Park

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:
subject: Controller servlet better practice
It's not a secret anymore!