If you have been looking for a good overview of Design Patterns in J2EE, look no further. In this article you will learn about Design Patterns and how they interact with J2EE's tier-based development. It covers three of the most common Design Patterns, and provides a real-world example of Design Patterns in action.
J2EE Design Patterns: Getting Started - J2EE Design Patterns in the Real World (Page 3 of 4 )
To implement the design patterns I will be using the solution for a Login Authentication problem. Three components implement the design patterns for this solution, which are:
LoginDAO – A Java class that implements the DAO pattern.
LoginController, login.jsp, welcome.jsp – The Controller and View of MVC pattern.
UserDTO – A Java Bean implementing the DTO pattern and working as the Model Component.
So let's get started.
First we'll do the DAO implementation. The implementation provides connection services.
package loginmodule.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; public class LoginDAO { Connection connection; Statement statement; PreparedStatement pStatement; //First comes the connectionand statement public DBConnection()throws Exception { Class.forName("org.hsqldb.jdbcDriver").newInstance(); connection=DriverManager .getConnection("jdbc:hsqldb:hsql://localhost");// connection=DriverManager.getConnection (dbConfig.getConnectString(),dbConfig.getDbUser (),dbConfig.getDbPass());// statement=connection.createStatement(); } //Then the getters and setters. It is through the getters and setters that //this DAO implementation provides its services public void setConnection(Connection connection) { this.connection = connection; } public Connection getConnection() { return connection; } public void setStatement(Statement statement) { this.statement = statement; } public Statement getStatement() { return statement; } public void setPStatement(PreparedStatement pStatement) { this.pStatement = pStatement; } public PreparedStatement getPStatement() { return pStatement; }