A question about read and pass data from servlet to JSP
I want to create a Java EE project. Totally, it has 5 files in this project.
- JSP Files: EmployeeInfo.jsp, EmployeeDetails.jsp
- Servlets: EmployeeRegistrationServlet, EmployeeSearchServlet
- Bean: Employee
EmployeeInfo.jsp file that gets either Employee Id or Employee Information. It have two Submit buttons. If user wants to search an Employee, an ID would be entered and it will call EmployeeSearchServlet, if user wants to add new Employee information, then EmployeeRegistrationServlet will be called with customer information entered. Here is the code:
EmployeeSearchServlet is supposed to search an employee with employee id that was entered by the end user. The job of the servlet is to find particular employee within a collection. Once the employee is found, the flow will be forwarded to EmployeeDetails.jsp.
EmployeeRegistrationServlet is supposed to add employees into a collection where store employees in memory.
If an employee is found, details of employee will be displayed on EmployeeDetails.jsp. If a new employee is added, details of employee will be displayed on EmployeeDetails.jsp.
Here is my EmployeeInfo.jsp
<body>
<h1 align="center">Employee Information</h1>
<!--Form of find employee by employeeID -->
<form action="EmployeeSearchServlet" method="POST">
<fieldset>
<legend>Find Employee</legend>
Employee ID: <input type="text" name="findEmployeeID"><br>
<input type="submit" name="findEmployeeSubmit" value="Find Employee">
</fieldset>
</form>
<p></p>
<!--Form of add employee-->
<form action="EmployeeRegistrationServlet" method="POST">
<fieldset>
<legend>Employee Information</legend>
<table>
<tr>
<td>Employee ID (key)*: </td>
<td><input type="text" name="employeeID"></td>
</tr>
<tr>
<td>First Name:* </td>
<td><input type="text" name="firstName"></td>
</tr>
<tr>
<td>Last Name:* </td>
<td><input type="text" name="lastName"></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Phone: </td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>Hire Date: </td>
<td><input type="text" name="hireDate"></td>
</tr>
<tr>
<td>Manager ID: </td>
<td><input type="text" name="managerID"></td>
</tr>
<tr>
<td>Department ID: </td>
<td><input type="text" name="departmentID"></td>
</tr>
</table>
<input type="submit" name="addEmployeeSubmit" value="Add Employee">
</fieldset>
</form>
</body>
Here is my Employee bean.
public class Employee {
private String employeeID;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private String hireDate;
private String managerID;
private String departmentID;
public Employee(String newEmployeeID,
String newFirstName,
String newLastName,
String newEmail,
String newPhoneNumber,
String newHireDate,
String newManagerID,
String newDepartmentID){
this.employeeID = newEmployeeID;
this.firstName = newFirstName;
this.lastName = newLastName;
this.email = newEmail;
this.phoneNumber = newPhoneNumber;
this.hireDate = newHireDate;
this.managerID = newManagerID;
this.departmentID = newDepartmentID;
}
public void setEmployeeID(String newEmployeeID){
this.employeeID = newEmployeeID;
}
public void setFirstName(String newFirstName){
this.firstName = newFirstName;
}
public void setLastName(String newLastName){
this.lastName = newLastName;
}
public void setEmail(String newEmail){
this.email = newEmail;
}
public void setPhoneNumber(String newPhoneNumber){
this.phoneNumber = newPhoneNumber;
}
public void setHireDate(String newHireDate){
this.hireDate = newHireDate;
}
public void setManagerID(String newManagerID){
this.managerID = newManagerID;
}
public void setDepartmentID(String newDepartmentID){
this.departmentID = newDepartmentID;
}
public String getEmployeeID() {
return employeeID;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getHireDate(){
return hireDate;
}
public String getManagerID(){
return managerID;
}
public String getDepartmentID(){
return departmentID;
}
}
here is my EmployeeRegistrationServlet
@WebServlet("/EmployeeRegistrationServlet")
public class EmployeeRegistrationServlet extends HttpServlet {
private Map<String,Employee> employees;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String employeeID = request.getParameter("employeeID").trim();
String firstName = request.getParameter("firstName").trim();
String lastName = request.getParameter("lastName").trim();
String email = request.getParameter("email").trim();
String phone = request.getParameter("phone").trim();
String hireDate = request.getParameter("hireDate").trim();
String managerID = request.getParameter("managerID").trim();
String departmentID = request.getParameter("departmentID").trim();
employees = new HashMap<String,Employee>();
Employee newAdd = new Employee(employeeID, firstName, lastName, email, phone, hireDate, managerID, departmentID);
request.getRequestDispatcher("../../../../WebContent/EmployeeDetail.jsp").forward(request,response);
}
}
My question is how to use EmployeeDetail.jsp read bean object passed from EmployeeRegistrationServlet? And how to Find an employee by searching the employee ID? Please give me some suggestions. Thanks a lot.