Controller Servlet help

I have an application which has a navigation page with 3 links and 3 DAOs and their corresponding JSP presentation pages.

My problem is how to make the controller instantiate the right DAO object and execute the right DAO method and send back the result in a VO. Can any one help me how I can do this the best way.

Note: I know how to implement a hidden variable and switch statement inside the servlet. I am looking for a method to avoid a big switch statement. Also if I implement a 4th DAO without changing the Servlet how to incorporate this into to the web presentation.

[594 byte] By [rnatsa] at [2007-9-28 5:56:46]
# 1
Use a DAO factory that takes the factory type as a parameter in the getFactory() method.
meadandalea at 2007-7-9 17:07:48 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
Do I then need to look up the correct factory in the JNDI context and instantiate that class?thanks in advance for the help
rnatsa at 2007-7-9 17:07:48 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

[nobr]I think Meads is right - you should pass something to the controller that can be used to look up the DAO factory:

<a href="go?dao=User">Users</a><br/>

<a href="go?dao=Content">Content Items</a><br/>

etc...

Your controller would then perhaps contain a Map from DAO name to DAO JNDI name:

String daoName = request.getParameter("dao");

String daoFactoryHomeJNDIName = (String)DAO_FACTORY_HOME_MAP.get(daoName);

DAOFactory daoFactory = ((DAOFactoryHome)initialContext.lookup(daoFactoryJNDIName)).create();

Collection vos = daoFactory.findAll();

request.setAttribtute("valueObjects", vos);

request.getRequestDispatcher(dao + "/list.jsp").forward(request, response);

Or something along those lines.

Your DAO map could be contained in Servlet initialisation parameters or a configuration file present on the classpath (perhaps read from a Properties file):

User=user.DAOFactoryHome

Content=content-items.DAOFactoryHome

Hope this helps.[/nobr]

KPSeala at 2007-7-9 17:07:48 > top of Java-index,Other Topics,Patterns & OO Design...