Home arrow Web Authoring arrow Page 3 - Completing an EAR
WEB AUTHORING

Completing an EAR


In the second of two parts, we focus on deploying the EAR we built in the previous article. This article is excerpted from chapter three of the book JBoss at Work: A Practical Guide, written by Tom Marrs and Scott Davis (O'Reilly; ISBN: 0596007345). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
July 10, 2007
TABLE OF CONTENTS:
  1. · Completing an EAR
  2. · Deploying the EAR
  3. · Adding a DAO
  4. · Using XDoclet

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Completing an EAR - Adding a DAO
(Page 3 of 4 )

In that spirit, let’s add another component that will pay dividends in future flexibility. Right now, your servlet is creating the car list each time a request comes in. This really isn’t optimal. Servlets should deal with the mechanics of the HTTP request/response cycle. They shouldn’t perform persistence tier tasks.

We aren’t quite ready to install a database (that happens in the next chapter), but we can lay the groundwork by creating a Data Access Object (DAO). A DAO is a layer of abstraction—it hides the actual persistence specifics behind a common interface.

The DAO we create in this chapter still stores the DTO objects in a simpleArrayList. In the next chapter, the DAO will pull car data from a database that uses JDBC. In the chapter after that, it will use Hibernate (an Object/Relational Mapper) to do the same thing. By getting the DAO in place now, however, we’ll be able to make these implementation changes without affecting presentation-tier code. Loose coupling and high cohesion comes to the rescue again.

TheCarDAOprovides afindAll()method that returns aListofCarDTOs. The source code in Example 3-7 can be found in the common directory in ch03b-dao.

Example 3-7.  CarDAO.java

package com.jbossatwork.dao;

import java.util.*;
import com.jbossatwork.dto.CarDTO;

public class CarDAO
{
   
private List carList;

   public CarDAO()
   {
      carList = new ArrayList();

      carList.add(new CarDTO("Toyota", "Camry", "2005"));
     
carList.add(new CarDTO("Toyota", "Corolla", "1999"));
     
carList.add(new CarDTO("Ford", "Explorer", "2005"));
   }

   public List findAll()
   
{

      return carList;

   }
}

The corresponding change in theControllerServlet calls the newly created DAO in Example 3-8.

Example 3-8.  ControllerServlet.java

// perform action
       
if(VIEW_CAR_LIST_ACTION.equals(actionName))
       

          
CarDAO carDAO = new CarDAO();
           request.setAttribute("carList", carDAO.findAll()); 

           destinationPage = "/carList.jsp";
        }

Not only does this change simplify the code in the servlet, it feels more correct as well. The servlet concerns itself solely with web mechanics and delegates the database tasks to a dedicated class. Another pleasant side effect of this is reuse—your data access code can now be called outside of the web tier. If a business tier object needs access to this data, it can make the same call that we make.

Build and deploy the code to verify that we haven’t broken your application with this change.


blog comments powered by Disqus
WEB AUTHORING ARTICLES

- Top WordPress Plugins for Social Media
- How to Install WordPress on WAMPP
- More of the Best Wordpress Plugins for Comme...
- Top Wordpress Plugins to Fight Spam
- More of the top Drupal modules for SEO
- Top Drupal Modules for SEO
- More Top Wordpress Plugins for SEO
- Website Optimization Tips
- Backup and Restore a Drupal Website and Data...
- 5 Best Web Font Services
- Web Font Embedding Services
- Joomla Site Administration
- Create PHP Pages that Use WordPress Templates
- Top Joomla Extensions for Social Networking
- Drupal Modules for Site Navigation

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials