Tuesday, 4 September 2012

Example on MVC Model 1 : Search & Create Employees

In my earlier post regarding  Java Database Connectivity, I created a database “EmployeeRecord” using Microsoft Access and created a table named “Employees”. Here we will be using JSP and javaBeans to search and add employees in the same table.

So for this small application, we will use JavaBeans, JSP and Error Page. Below is list and short description of these files:

EmployeeInfo - has attributes: EmpId, FirstName, LastName, Salary
EmployeeDB - encapsulates DB logic (used to save & retrieve EmployeeInfo data).

Java Server Pages:-
addemployee.jsp - collect new employee info that will be saved in database.
saveperson.jsp - Receives employee info from addemployee.jsp and after that saves it to database.
searchemployee.jsp - provide search criteria to search employee’s info by providing first name.
showemployee.jsp - receive employee’s name from searchemployee.jsp to search in database. Then retrieves and displays employee record.

Error Page:-
emperror.jsp – This page is declared as an error page and used to identify the type of exception. It also displays the message associated with the received exception to the user.

Program Flow
The following figure helps to understand the program flow of this example:

1) addemployee.jsp takes employee’s information from the user and sends it to saveemployee.jsp. After receiving request, saveemployee.jsp makes an object of EmployeeInfo using received information and saves it into the database using EmployeeDB Java bean.

2) searchemployee.jsp takes search criteria from user and passes it to showemployee.jsp that searches the record in database using EmployeeDB and shows results to the user.

3) If any uncaught exception is generated on these JSP, emperror.jsp is called implicitly, which displays an appropriate message to the user after identifying the exception type.

Code for this Application
You can download these JSPs and java files from below links:

3) addemployee.jsp                                   4) searchemployee.jsp

1 comment: