系列文章目录:
目录
4.新建UpdateStudentServlet.java类
5.新建ToStudentModifyServlet.java类
前言:
上一期内容我们已经完成了对该系统整体框架的搭建以及课程管理模块的编写,那么本期内容我们就来实现学生模块的编写,将这个系统完全实现。
一、编写StudentDao逻辑部分代码
1.新建StudentDao.java类
(1)在包csdn.ah.dao包下新建StudentDao.java类,用于存储学生数据
(2)编写代码
package csdn.ah.dao;
import java.util.ArrayList;
import csdn.ah.pojo.Student;
import csdn.ah.pojo.Subject;
public class StudentDao {
private static ArrayList<Student> list = new ArrayList<Student>();
private static int index=0;
public static ArrayList<Student> studentList(){
return list;
}
public static void addStudent(String name,String gender,int subjectId) {
index++;
Subject subject = SubjectDao.getById(subjectId);
Student student = new Student(index, name, gender, subject);
list.add(student);
}
public static Student getById(int id) {
Student student = null;
for(Student stu:list) {
if(stu.getId()==id) {
student = stu;
break;
}
}
return student;
}
public static void updateStudent(int id,String name,String gender,int subjectId) {
for(Student stu:list) {
if(stu.getId()==id) {
stu.setName(name);
stu.setGender(gender);
Subject subject = SubjectDao.getById(subjectId);
stu.setSubject(subject);
break;
}
}
}
public static void deleteStudent(int id) {
for(int i=0;i<list.size();i++) {
if(list.get(i).getId()==id) {
list.remove(i);
break;
}
}
}
}
注:代码可能暂时报错,因为我们pojo包下的Student类还未编写。
二、编写Student.java 类
1.新建Student.java类
(1)在csdn.ah.pojo包下新建Student.java类,用于定义一个学生对象应有的属性
(2)编写代码
package csdn.ah.pojo;
public class Student {
private int id;
private String name;
private String gender;
private Subject subject;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name, String gender, Subject subject) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.subject = subject;
}
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;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
}
三、编写Student的Servlet相关代码
1.新建AddStudentServlet.java
(1)在csdn.ah.servlet包下新建AddStudentServlet.java类,用于实现添加一个学生对象的逻辑
(2)编写代码
package csdn.ah.servlet;
import java.io.IOException;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;
import csdn.ah.dao.StudentDao;
import csdn.ah.pojo.Student;
/**
* Servlet implementation class AddStudentServlet
*/
@WebServlet("/addStudent")
public class AddStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddStudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String subjectIdStr = request.getParameter("subjectId");
int subjectId = Integer.parseInt(subjectIdStr);
StudentDao.addStudent(name, gender, subjectId);
ArrayList<Student> list = StudentDao.studentList();
HttpSession session = request.getSession();
session.setAttribute("studentList", list);
response.sendRedirect("studentList.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
2.新建DeleteStudentServlet.java
(1)在csdn.ah.servlet包下新建DeleteStudentServlet.java类,用于实现删除某个学生对象的逻辑
(2)要点二
package csdn.ah.servlet;
import java.io.IOException;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;
import csdn.ah.dao.StudentDao;
import csdn.ah.pojo.Student;
/**
* Servlet implementation class DeleteStudentServlet
*/
@WebServlet("/deleteStudent")
public class DeleteStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteStudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取参数
String idStr = request.getParameter("id");
int id = Integer.parseInt(idStr);
//2.删除学生数据
StudentDao.deleteStudent(id);
//3.查询最新学生列表
ArrayList<Student> list = StudentDao.studentList();
HttpSession session = request.getSession();
session.setAttribute("studentList", list);
//4.重定向学生管理页
response.sendRedirect("studentList.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
3.新建StudentListServlet.java类
(1)在csdn.ah.servlet包下新建StudentListServlet.java类,用于展示所有学生对象数据
(2)编写代码
package csdn.ah.servlet;
import java.io.IOException;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;
import csdn.ah.dao.StudentDao;
import csdn.ah.pojo.Student;
/**
* Servlet implementation class StudentListServlet
*/
@WebServlet("/studentList")
public class StudentListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentListServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<Student> list = StudentDao.studentList();
HttpSession session = request.getSession();
session.setAttribute("studentList", list);
response.sendRedirect("studentList.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
4.新建UpdateStudentServlet.java类
(1)在包csdn.ah.servlet包下新建UpdateStudentServlet.java类,用于更新学生管理数据
(2)编写代码
package csdn.ah.servlet;
import java.io.IOException;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;
import csdn.ah.dao.StudentDao;
import csdn.ah.pojo.Student;
/**
* Servlet implementation class UpdateStudentServlet
*/
@WebServlet("/updateStudent")
public class UpdateStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateStudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取参数
String idStr = request.getParameter("id");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String subjectIdStr = request.getParameter("subjectId");
int id = Integer.parseInt(idStr);
int subjectId = Integer.parseInt(subjectIdStr);
//2.修改学生
StudentDao.updateStudent(id, name, gender, subjectId);
//3.查询最新学生列表
ArrayList<Student> list = StudentDao.studentList();
HttpSession session = request.getSession();
session.setAttribute("studentList", list);
//4.重定向学生管理页
response.sendRedirect("studentList.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
5.新建ToStudentModifyServlet.java类
(1)在包csdn.ah.servlet下新建ToStudentModifyServlet.java类,用于跳转到学生信息修改页面,并实现修改学生数据的功能
(2)编写代码
package csdn.ah.servlet;
import java.io.IOException;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;
import csdn.ah.dao.StudentDao;
import csdn.ah.dao.SubjectDao;
import csdn.ah.pojo.Student;
import csdn.ah.pojo.Subject;
/**
* Servlet implementation class ToStudentModify
*/
@WebServlet("/toStudentModify")
public class ToStudentModifyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ToStudentModifyServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String idStr = request.getParameter("id");
int id = Integer.parseInt(idStr);
Student student = StudentDao.getById(id);
ArrayList<Subject> list = SubjectDao.subjectList();
HttpSession session = request.getSession();
session.setAttribute("student", student);
session.setAttribute("subjectList", list);
response.sendRedirect("studentModify.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
6.新建ToStudentAddServlet.java类
(1)在包csdn.ah.servlet下新建ToStudentAddServlet.java类,用于跳转到学生添加页面,并实现添加学生对象
(2)编写代码
package csdn.ah.servlet;
import java.io.IOException;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;
import csdn.ah.dao.SubjectDao;
import csdn.ah.pojo.Subject;
/**
* Servlet implementation class ToStudentAdd
*/
@WebServlet("/toStudentAdd")
public class ToStudentAddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ToStudentAddServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<Subject> list = SubjectDao.subjectList();
HttpSession session = request.getSession();
session.setAttribute("subjectList", list);
response.sendRedirect("studentAdd.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
四、运行测试
1.测试学生管理页面能否正常跳转
2.测试新增学生功能是否正常
<%@page import="java.util.ArrayList"%>
<%@page import="csdn.ah.pojo.Subject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<%
ArrayList<Subject> list = (ArrayList<Subject>)session.getAttribute("subjectList");
String optionStr = "";
if(list!=null){
for(Subject subject :list){
optionStr+="<option value='"+subject.getId()+"'>"+subject.getName()+"</option>";
}
}
%>
<form action="addStudent">
姓名:<input type="text" name="name"><br>
性别:
<select name="gender">
<option value='0'>请选择性别</option>
<option value='男'>男</option>
<option value='女'>女</option>
</select><br>
课程:
<select name="subjectId">
<option value='0'>请选择课程</option>
<%=optionStr %>
</select><br>
<input type="submit" value="新增">
</form>
</body>
</html>
<%@page import="csdn.ah.pojo.Subject"%>
<%@page import="csdn.ah.pojo.Student"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生管理</title>
</head>
<body>
<%
ArrayList<Student> list =(ArrayList<Student>)session.getAttribute("studentList");
String trStr = "";
if(list != null){
for(Student student :list){
trStr +="<tr>"+
"<td>"+student.getId()+"</td>"+
"<td>"+student.getName()+"</td>"+
"<td>"+student.getGender()+"</td>"+
"<td>"+student.getSubject().getName()+"</td>"+
"<td><a href='toStudentModify?id="+student.getId()+"'>修改</a> <a href='deleteStudent?id="+student.getId()+"'>删除</a></td>"+
"</tr>";
}
}
%>
<a href="index.html">首页</a> <a href="toStudentAdd">新增学生</a><br/>
<table style="width:100%;text-align:center">
<tr>
<th>编号</th>
<th>名称</th>
<th>性别</th>
<th>课程</th>
<th>操作</th>
</tr>
<%=trStr %>
</table>
</body>
</html>
<%@page import="csdn.ah.pojo.Student"%>
<%@page import="java.util.ArrayList"%>
<%@page import="csdn.ah.pojo.Subject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<%
ArrayList<Subject> list =(ArrayList<Subject>) session.getAttribute("subjectList");
Student student =(Student) session.getAttribute("student");
String optionStr = "";
if(list != null){
for(Subject subject : list){
if(subject.getId()==student.getSubject().getId()){
optionStr+="<option value='"+subject.getId()+"' selected>"+subject.getName()+"</option>";
}else{
optionStr+="<option value='"+subject.getId()+"'>"+subject.getName()+"</option>";
}
}
}
%>
<form action="updateStudent">
<input type="hidden" name="id" value="<%=student.getId() %>">
姓名:<input type="text" name="name" value="<%=student.getName() %>"><br>
性别:
<select name="gender">
<option value='0'>请选择性别</option>
<option value='男' <%=student.getGender().equals("男")?"selected":"" %>>男</option>
<option value='女'<%=student.getGender().equals("女")?"selected":"" %>>女</option>
</select><br>
课程:
<select name="subjectId">
<option value='0'>请选择课程</option>
<%=optionStr %>
</select><br>
<input type="submit" value="修改">
</form>
</body>
</html>
再次重启项目运行测试:
3.测试修改学生信息功能是否正常 ![](https://i-blog.csdnimg.cn/blog_migrate/4dd66ef318598b027a9aa54ee3ac5e93.png)
4.测试学生信息删除功能是否正常
5.测试页面跳转
总结:
至此,我们关于静态学生管理系统的项目已经全部实现。现在,你是否有一些小成就呢?我想大家应该还在疑惑,虽然项目跟着做完了,但是还是弄不明白是怎么实现的,实际上大家需要关注的是Servlet类的实现和部分JSP页面存在的逻辑代码,弄清这个那么这样一个项目我们就测地完成了。下一期内容将会带大家将此系统升级为JDBC版本,让前端后端与数据库联动起来,实现真正的企业级开发。关注我,获取最新内容。