Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Abstract Factory DAO Pattern in JSP Page

Thread view: 
Dave - 26 Jan 2007 21:57 GMT
I have implemented the J2EE abstract factory DAO pattern for accessing data
in my application. This works well in my Java application.

http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html

I am, however, having problems using this with my JSP page. As objects are
typically instantiated using the <jsp:usebean...> tag in JSP, and this
instantiates with a default constructor how do I use this pattern with my
JSP page?

In my Java application I use code along the lines of:

/* The DAOFactory should be instantiated once only! */
DAOFactory daoFactory = DAOFactory.getDAOFactory(DAOFactory.SQLSERVER);

PersonDAO personDAO = daoFactory.getPersonDAO();

Person person = personDAO.getPerson(1234);

I *cannot* instantiate a DAOFactory object using <jsp:usebean..> because it
is an abstract class. I also *cannot* instantiate a PersonDAO object without
using the factory as it is an interface.

My initial thoughts are to use some sort of intermediate wrapper class that
instantiates the DAOFactory and handles requests. Is this the standard way
to handle this problem?

Any suggestions would be appreciated.
Lew - 26 Jan 2007 23:24 GMT
> I *cannot* instantiate a DAOFactory object using <jsp:usebean..> because it
> is an abstract class. I also *cannot* instantiate a PersonDAO object without
[quoted text clipped - 3 lines]
> instantiates the DAOFactory and handles requests. Is this the standard way
> to handle this problem?

There are at least a couple of ways to handle this.

One way leverages the MVC pattern, whereby the logic class associated with the
screen places key objects into the request via setAttribute():

import entity.Person;
public class PersonalInfoHandler extends AbstractHandler
{
   private final PersonDao dao = (new DaoFactory()).getPersonDao();

   @Override
   public void handle()
   {
      Person person = dao.getPerson( request.getParameter( "person" ));
      request.setAttribute( "person", person );
   )
}

The dispatch servlet calls the handle() method (through an AbstractHandler
instance), which in turn places the found Person into the request attributes.
(Error checking omitted for simplicity.)

In the associated JSP one can use expressions like:

     <p>Person name: ${person.name}</p>

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.