三层架构(MVC)完成学生管理系统
包含了前端,Servlet,数据库等
使用Eclipse+Tomcat+MySQL完成
效果图:
首页:
增加,删除,更新,查看功能可以正常使用
增加功能:
更新功能:
详情页:
并未优化前端页面。
代码:
代码结构:
前端代码:
index.jsp
<%@page import="org.student.entity.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index</title>
<style type="text/css">
table {
width: 90%;
background: #ccc;
margin: 10px auto;
border-collapse: collapse;/*border-collapse:collapse合并内外边距(去除表格单元格默认的2个像素内外边距*/
}
th{
height: 25px;
line-height: 25px;
text-align: center;
border: 1px solid #ccc;
}
th {
background: #eee;
font-weight: normal;
}
tr {
background: #fff;
}
tr:hover {
background: #cc0;
}
</style>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
List<Student> students = (List<Student>)request.getAttribute("students");
%>
<table>
<tr>
<th>Student No</th>
<th>Student Name</th>
<th>Student Age</th>
<th>Student Address</th>
<th>Delete student</th>
<th>Update student</th>
<th>Details page</th>
<tr>
<%
for(Student student:students){
%>
<tr>
<th><%=student.getSno() %></th>
<th><%=student.getSname() %></th>
<th><%=student.getSage() %></th>
<th><%=student.getSaddress() %></th>
<th><a href="DeleteStudentServlet?sno=<%=student.getSno()%>">Delete</a></th>
<th><a href="update.jsp?sno=<%=student.getSno()%>">Update</a></th>
<th><a href="QueryStudentBySnoServlet?sno=<%=student.getSno()%>">Details</a></th>
</tr>
<%
}
%>
</table>
<a href="add.jsp" style="display: block; text-align: center;">Add Student</a>
</body>
</html>
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Student</title>
</head>
<body>
Please enter the student Information of the student you want to add:
<form action="AddStudentServlet" method="post">
Number:<input type="text" name="sno"><br/>
Name:<input type="text" name="sname"><br/>
Age:<input type="text" name="sage"><br/>
Address:<input type="text" name="saddress"><br/>
<input type="submit" value="add"><br/>
</form>
</body>
</html>
update.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Update Student Information</title>
</head>
<body>
<%
int sno = Integer.parseInt(request.getParameter("sno"));
%>
<form action="UpdateStudentServlet" method="post">
Please enter the student number of the student to be modified:<input type="text" name="sno" value=<%=sno %> readonly="readonly"><br/>
Please enter the modified Name:<input type="text" name="sname"><br/>
Please enter the modified age:<input type="text" name="sage"><br/>
Please enter the modified address:<input type="text" name="saddress"><br/>
<input type="submit" value="修改"><br/>
</form>
</body>
</html>
detailsPage.jsp
<%@page import="org.student.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Student Details Page</title>
</head>
<body>
<%
Student student = (Student)request.getAttribute("student");
%>
Student No Is : <%=student.getSno() %><br/>
Student Name Is : <%=student.getSname() %><br/>
Student Age Is : <%=student.getSage() %><br/>
Student Address Is : <%=student.getSaddress() %><br/>
</body>
</html>
后端代码:
实体类:
package org.student.entity;
//实体类
public class Student {
private int sno;
private String sname;
private int sage;
private String saddress;
public Student() {
}
public Student(String sname, int sage, String saddress) {
this.sname = sname;
this.sage = sage;
this.saddress = saddress;
}
public Student(int sno, String sname, int sage, String saddress) {
this.sno = sno;
this.sname = sname;
this.sage = sage;
this.saddress = saddress;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSaddress() {
return saddress;
}
public void setSaddress(String saddress) {
this.saddress = saddress;
}
public String toString() {
return this.getSno()+"--"+this.getSname()+"--"+this.getSage()+"--"+this.getSaddress();
}
}
AddStudentServlet
package org.sudent.servlet;
//表示层:Servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.student.entity.Student;
import org.student.service.StudentService;
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddStudentServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
response.setCharacterEncoding("utf-8");
int no = Integer.parseInt(request.getParameter("sno")) ;
String name = request.getParameter("sname");
int age = Integer.parseInt(request.getParameter("sage"));
String address = request.getParameter("saddress");
Student student = new Student(no,name,age,address);
StudentService studentService = new StudentService();
boolean rs = studentService.addStudent(student);
if(r