Business Delegate Design Pattern
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index Page</title>
</head>
<body>
<a href="/javaT/registerStudent.jsp">Show Student Registration Information</a>
</body>
</html>
registerStudent.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register Student</title>
</head>
<body>
<form action="register">
<table>
<tr><td>Student Id : </td><td> <input type="text" name="studentId"></td></tr>
<tr><td> Name : </td><td> <input type="text" name="name"></td></tr>
<tr><td>Course Id : </td><td> <input type="text" name="courseId"></td></tr>
<tr><td colspan="2"> <input type="submit" value="Register"></td></tr>
</table>
</form>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error !!!</title>
</head>
<body>
<p style="color:red;">
Error while processing your request. Please contact administration.
</p>
</body>
</html>
confirm.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Successfully</title>
</head>
<body>
<p style="color:green;">
Student ${studentId} register successfully ....
</p>
</body>
</html>
package business.delegate;
import java.io.Serializable;
public class StudentBO implements Serializable {
private int id;
private String name;
//getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package business.delegate;
import java.io.Serializable;
public class StudentCourseBO implements Serializable {
private int studentId;
private int courseId;
// setters and getters
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
}
db.properties
db.driverClassName = oracle.jdbc.driver.OracleDriver
db.url = jdbc:oracle:thin:@localhost:1521:xe
db.userName = system
db.password = tiger
package business.delegate;
import java.sql.Connection;
import java.sql.SQLException;
public interface StudentDao {
void insert(StudentBO studentBO, Connection con) throws SQLException;
}
package business.delegate;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Logger;
public class StudentDaoImpl implements StudentDao {
//SQL queries
private final String INSERT_QUERY="insert into student(student_id,name) values(?,?)";
private static Logger logger;
static {
logger=Logger.getLogger("log_file");
}
@Override
public void insert(StudentBO studentBO, Connection con) throws SQLException {
PreparedStatement pstmt=null;
try{
pstmt=con.prepareStatement(INSERT_QUERY);
pstmt.setInt(1, studentBO.getId());
pstmt.setString(2, studentBO.getName());
pstmt.executeUpdate();
}catch(SQLException e){
logger.throwing("StudentDao", "Unable to perform insert", e);
throw e;
}finally{
if(pstmt != null)
pstmt.close();
}
}
}
/*
create table student(student_id number(20),name varchar2(15));
*/
package business.delegate;
import java.sql.Connection;
import java.sql.SQLException;
public interface StudentCourseDao {
void insert(StudentCourseBO studentCourseBO, Connection con) throws SQLException;
}
package business.delegate;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Logger;
public class StudentCourseDaoImpl implements StudentCourseDao {
//SQL queries
private final String INSERT_QUERY="insert into course(student_id, course_id) values(?,?)";
private static Logger logger;
static {
logger=Logger.getLogger("log_file");
}
@Override
public void insert(StudentCourseBO studentCourseBO, Connection con) throws SQLException {
PreparedStatement pstmt=null;
try{
pstmt=con.prepareStatement(INSERT_QUERY);
pstmt.setInt(1, studentCourseBO.getStudentId());
pstmt.setInt(2, studentCourseBO.getCourseId());
pstmt.executeUpdate();
}catch(SQLException e){
logger.throwing("StudentCourseDao", "Unable to perform insert", e);
throw e;
}finally{
if(pstmt != null)
pstmt.close();
}
}
}
/*
create table course(student_id number(20), course_id number(20));
*/
package business.delegate;
import java.sql.SQLException;
public interface RegistrationDelegate {
void register(RegistrationVO registrationVO) throws SQLException, ClassNotFoundException, GenericException;
}
package business.delegate;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Logger;
public class RegistrationDelegateImpl implements RegistrationDelegate {
private static Logger logger;
static{
logger=Logger.getLogger("log_file");
}
@Override
public void register(RegistrationVO registrationVO) throws SQLException, ClassNotFoundException, GenericException {
boolean isEx=false;
Connection con=null;
StudentBO studentBO=null;
StudentCourseBO studentCourseBO=null;
StudentDao studentDao=null;
StudentCourseDao studentCourseDao=null;
try{
con=ConnectionFactory.getConnection();
studentBO=new StudentBO();
studentBO.setId(Integer.parseInt(registrationVO.getStudentId()));
studentBO.setName(registrationVO.getName());
studentDao=new StudentDaoImpl();
studentDao.insert(studentBO, con);
studentCourseBO=new StudentCourseBO();
studentCourseBO.setStudentId(Integer.parseInt(registrationVO.getStudentId()));
studentCourseBO.setCourseId(Integer.parseInt(registrationVO.getCourseId()));
studentCourseDao=new StudentCourseDaoImpl();
studentCourseDao.insert(studentCourseBO, con);
}catch(SQLException e){
logger.throwing("RegistrationDelegateImpl", "Unable to register student", e);
throw new GenericException(e);
}catch (ClassNotFoundException e) {
logger.throwing("RegistrationDelegateImpl", "Unable to register student", e);
throw new GenericException(e);
}finally{
if(con != null) {
try{
if(isEx)
con.rollback();
else
con.commit();
con.close();
}catch(SQLException e){
logger.throwing("RegistrationDelegateImpl", "Unable to commit/rollback/clode connection", e);
throw new GenericException(e);
}
}//if
}//finally
}
}
package business.delegate;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegistrationServlet extends HttpServlet {
private static Logger logger=Logger.getLogger("log_file");
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
boolean isEx=false;
String studentId=null;
String name=null;
String courseId=null;
String page=null;
RegistrationVO registrationVO=null;
RegistrationDelegate registrationDelegate=null;
RequestDispatcher requestDispatcher=null;
studentId=request.getParameter("studentId");
name=request.getParameter("name");
courseId=request.getParameter("courseId");
registrationVO=new RegistrationVO();
registrationVO.setStudentId(studentId);
registrationVO.setName(name);
registrationVO.setCourseId(courseId);
registrationDelegate=new RegistrationDelegateImpl();
try{
registrationDelegate.register(registrationVO);
request.setAttribute("studentId", studentId);
}catch(GenericException | ClassNotFoundException | SQLException e){
isEx=true;
logger.throwing("Registration Servlet", "Error occured during registration", e);
}
if(isEx)
page="error.jsp";
else
page="confirm.jsp";
requestDispatcher=request.getRequestDispatcher(page);
requestDispatcher.forward(request, response);
}
}
package business.delegate;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Logger;
public class ConnectionFactory {
private static Properties props;
private static Logger logger;
static{
logger=Logger.getLogger("log_file");
props=new Properties();
ResourceBundle rb=ResourceBundle.getBundle("business/delegate/db"); // business.delegate.db
Set<String> keys=rb.keySet();
for(String key:keys){
props.put(key, rb.getString(key));
}
}
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Connection con=null;
try{
Class.forName(props.getProperty("db.driverClassName"));
con=DriverManager.getConnection(props.getProperty("db.url"),props.getProperty("db.userName"),props.getProperty("db.password"));
con.setAutoCommit(false);
}catch(SQLException e){
logger.throwing("ConnectionFactory", "Unable to get Connection", e);
throw e ;
}catch (Exception e) {
logger.throwing("ConnectionFactory", "Unable to load driver class", e);
throw e ;
}
return con;
}
}
package business.delegate;
public class GenericException extends Throwable {
public GenericException(){
super();
}
public GenericException(String str, Throwable exp){
super(str,exp);
}
public GenericException(String str){
super(str);
}
public GenericException(Throwable exp){
super(exp);
}
}
package business.delegate;
import java.io.Serializable;
public class RegistrationVO implements Serializable{
private String studentId;
private String name;
private String courseId;
// setters and getters
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
}
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>BusinessDelegate</display-name>
<servlet>
<servlet-name>delegate</servlet-name>
<servlet-class>business.delegate.RegistrationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>delegate</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
output :
http://localhost:8001/javaT/
(OR)
http://localhost:8001/javaT/register?studentId=123&name=ashok&courseId=432
Student 123 register successfully ....
//add required jar files to project lib flder like servlet-api.jar, ojdbc14.jar etc.,
|