jdbc+jsp Implement Simple Employee Management System
- 2021-06-29 11:12:18
- OfStack
Simple page analysis
In the previous article, the simple database connection test has been tested for simple interaction with the database, that is, the implementation of the dao layer. Next, we will talk about the simple implementation of action. struts is used as the exchange between the presentation layer and server in ssh, but here I am not talking about struts, which is simply using the code written by jsp to transfer data.This is also the most tedious step, but it gives us a simple understanding of the underlying invocation. Here is the direct invocation of encapsulated data, exchange and use, the first thing to write is the use of action, the most used is the submission of getParameter forms, submit a form on the network here, and then obtain it through getParameter.Then add, delete, and check through the methods in enployeeDao, you can do basic logical operations.
code implementation
<%@page import="dao.EmployeeDao"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="entity.Employee"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>Insert title here</title>
</head>
<body>
<%
//1 Receive form submission parameters
String action = request.getParameter("action");
//3. call EmployeeDao in addEmployee(Employee employee) Complete employee addition
EmployeeDao employeeDao = new EmployeeDao();
if (action.equals("0") || action.equals("1")) {
// Add to To update
String empno = request.getParameter("empno");
String ename = request.getParameter("ename");
String sal = request.getParameter("sal");
String hiredate = request.getParameter("hiredate");
//2. Encapsulate data to Employee In object
Employee employee = new Employee();
employee.setEmpno(Integer.parseInt(empno));
employee.setEname(ename);
employee.setSal(Double.parseDouble(sal));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
employee.setHiredate(sdf.parse(hiredate));
if (action.equals("0")) {
// Add to
employeeDao.addEmployee(employee);
} else {
// To update
employeeDao.updateEmployee(employee);
}
} else if (action.equals("2")) {
// delete
String empno = request.getParameter("empno");
employeeDao.deleteEmployee(empno);
} else if (action.equals("3")) {
// Bulk Delete
System.out.println("action="+action);
String[] chks=request.getParameterValues("chks");
for(String chk:chks){
System.out.println("chk="+chk);
}
}
//4. Picture Jump To employeeList.jsp redirect
response.sendRedirect("employeeList.jsp");
%>
</body>
</html>
Then we did a simple page design, using the form of a table, the code is as follows
<%@page import="java.util.List"%>
<%@page import="entity.Employee"%>
<%@page import="dao.EmployeeDao"%>
<%@page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>Insert title here</title>
<script>
function checkAll() {
//1. Obtain chkAll Selected state of checked
var chkAll = document.getElementById("chkAll");
//alert(chkAll.checked);
//2 , get everything else checkbox
var chks = document.getElementsByName("chks");
for (var i = 0; i < chks.length; i++) {
//3 , will chkAll Selected state of checkbox Selected state synchronization for
chks[i].checked = chkAll.checked;
}
}
function onDelete(empno){
var result=confirm("Delete Employee?");
if(result){
window.location.href="employeeManageAction.jsp?action=2&empno=" rel="external nofollow" +empno;
}
}
//function onDeleteBatch(){
// Determine if there is a selection checkbox
// Selected occasions
// Prompt for deletion of confirmation information
// confirm deletion
// Submit Form
//var action=document.getElementById("action");
//action.value="3";
// document.frmlist.action="employeeManageAction.jsp"
// document.frmlist.submit();
// Cancel Delete
// Unhandled
// Unselected occasion
// Prompt for selected record information
//}
</script>
</head>
<body>
<h3>Employee List Page</h3>
<form name="frmSearch" action="xxxxAction.jsp" method="post">
ename:<input type="text" /> <input type="submit" name="btnSubmit"
value="submit" />
</form>
<P>
<hr />
<a href="employeeManage.jsp?action=0" rel="external nofollow" >Add Employee</a>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" onclick="onDeleteBatch();">Delete
Employee Batch</a>
<P>
<%
EmployeeDao emplyeeDao = new EmployeeDao();
List<Employee> employees = emplyeeDao.getEmployees();
%>
<%
if (employees != null && employees.size() > 0) {
%>
<form name="frmlist" action="" method="post">
<input type="hidden" name="action" id="action" value="" />
<table width="600px" border="1px">
<tr bgcolor="#009966">
<td><input type="checkbox" id="chkAll" onclick="checkAll()" /></td>
<td>empno</td>
<td>ename</td>
<td>sal</td>
<td>hiredate</td>
<td>action</td>
</tr>
<%
Employee employee = null;
for (int i = 0; i < employees.size(); i++) {
employee = employees.get(i);
%>
<tr>
<td><input type="checkbox" id="chk<%=i + 1%>" name="chks"
value="<%=employee.getEmpno()%>" /></td>
<td><%=employee.getEmpno()%></td>
<td><%=employee.getEname()%></td>
<td><%=employee.getSal()%></td>
<td><%=employee.getHiredate()%></td>
<td><a
href="employeeManage.jsp?action=1&empno=<%=employee.getEmpno()%>" rel="external nofollow" >update</a>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow"
onclick="onDelete(<%=employee.getEmpno()%>);">delete</a></td>
</tr>
<%
}
%>
</table>
</form>
<%
}
%>
</body>
</html>
The next thing to say is the update operation
<%@page import="entity.Employee"%>
<%@page import="dao.EmployeeDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>Insert title here</title>
<script>
function checkForm() {
var empno = document.getElementById("empno");
if (empno && empno.value == "") {
empno.focus();
alert("please input empno");
return;
}
var ename = document.getElementById("ename");
if (ename && ename.value == "") {
ename.focus();
alert("please input ename");
return;
}
var sal = document.getElementById("sal");
if (sal && sal.value == "") {
sal.focus();
alert("please input sal");
return;
}
var hiredate = document.getElementById("hiredate");
if (hiredate && hiredate.value == "") {
hiredate.focus();
alert("please input hiredate");
return;
}
var frm = document.getElementById("frm");
frm.submit();
}
</script>
</head>
<body>
<%
String action=request.getParameter("action");
Employee employee=null;
if(action.equals("1")){
// update operation
String empno = request.getParameter("empno");
EmployeeDao employeeDao = new EmployeeDao();
employee=employeeDao.getEmployeeByEmpno(empno);
}
%>
<h3>Employee <%=action.equals("1")?"Update":"Regist" %> Page</h3>
<form id="frm" action="employeeManageAction.jsp" method="post"
onsubmit="return checkForm();">
<input type="hidden" name="action" value="<%=action%>"/>
<table>
<tr>
<td>empno</td>
<td><input type="text" id="empno" name="empno" <%=action.equals("1")?"readOnly":"" %> value="<%=employee==null?"":employee.getEmpno()%>"/></td>
</tr>
<tr>
<td>ename</td>
<td><input type="text" id="ename" name="ename" value="<%=employee==null?"":employee.getEname()%>"/></td>
</tr>
<tr>
<td>sal</td>
<td><input type="text" id="sal" name="sal" value="<%=employee==null?"":employee.getSal()%>"/></td>
</tr>
<tr>
<td>hiredate</td>
<td><input type="text" id="hiredate" name="hiredate" value="<%=employee==null?"":employee.getHiredate()%>"/></td>
</tr>
<tr>
<td><input type="button" name="btnSubmit" value="submit"
onclick="checkForm();" /></td>
<td><input type="reset" name="btnReset" value="reset" /></td>
</tr>
</table>
</form>
</body>
</html>
In this process, we just used a simple js to make non-empty and data type judgments. This is the logic we need to learn, the business layer, and then do other things. Of course, I commented on several deletions and fuzzy searches that have not been completed, so I will perfect this next blog.
summary
During the learning process, I learned more about data invocation than configuring profiles like hibernate, then calling directly, and then using jsp, I also found that jsp embedded code is more cumbersome, which is another aspect of my learning.
For more learning materials, please pay attention to the theme "Management System Development".