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;
}
public void close() {
try {
connection.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
Next is the View implementation. It is just a JSP page providing the login and password fields for the user input.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
harset=windows-1252"></meta>
<title>login</title>
<link href="css/jdeveloper.css" rel="stylesheet" media="screen"/>
<link href="css/oracle.css" rel="stylesheet" media="screen"/>
</head>
<body class="APPSWINDOW"><form name="login"
method="post"><table cellspacing="2" cellpadding="3" border="1"
width="100%"
class="clsContentsBody">
<tr>
<th width="48%">
user name
</th>
<td width="52%">
<input type="text" name="user" class="GUITag"/>
</td>
</tr>
<tr>
<th width="48%">
password
</th>
<td width="52%">
<input type="password" name="passwd" class="GUITag"/>
</td>
</tr>
<tr>
<th width="48%">
</th>
<td width="52%">
<input type="submit" name="submit" value="go"/>
<input type="reset" name="reset" value="reset"/>
</td>
</tr>
</table></form></body>
</html>
Next: View component and DTO >>
More JavaScript Articles
More By A.P.Rajshekhar