Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
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 simple ArrayList. 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.