系列文章目录:
目录
前言:
在前面的五章内容当中,我们具体学习了本期往后内容需要用的一些基础知识,那么本章内容开始我们就来尝试搭建一个静态的学生管理系统。该系统内容较多,将分为几期实现。
一、程序设计构想
1.功能介绍
2.页面关系
二、程序框架的搭建
1.首页
(1)新建Dynamic Web项目,命名StudentManage,新建HTML页面,命名为index.html
(2)编写首页代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>功能管理</title>
</head>
<body>
功能管理<br>
<a href="subjectList.jsp">课程管理</a>
<a href="studentList.jsp">学生管理</a>
</body>
</html>
2.课程管理与学生管理
(1)课程管理
- 新建JSP页面命名为subjectList.jsp
- 编写代码
<%@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 trStr = "";
if(list != null){
for(Subject subject:list){
trStr+="<tr>"+
"<td>"+subject.getId()+"</td>"+
"<td>"+subject.getName()+"</td>"+
"<td>"+subject.getCredit()+"</td>"+
"<td><a href='"+"toSubjectModify?id="+subject.getId()+"'>修改</a> <a href='"+"deleteSubject?id="+subject.getId()+"'>删除</a></td>"+
"</tr>";
}
}
%>
<a href="index.html">首页</a> <a href="subjectAdd.jsp">新增课程</a><br/>
<table style="width:100%;text-align:center">
<tr>
<th>编号</th>
<th>名称</th>
<th>学分</th>
<th>操作</th>
</tr>
<%=trStr %>
</table>
</body>
</html>
注:这里代码可能会报错,这是由于我们的完整页面还没有搭建起来,因此报错。 这里暂时先不管,我们先将这部分页面搭建起来。
(2)学生管理
- 新建studentList.jsp页面
- 编写代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生管理</title>
</head>
<body>
<a href="index.html">首页</a> <a href="studentAdd.jsp">新增学生</a><br/>
<table style="width:100%;text-align:center">
<tr>
<th>编号</th>
<th>名称</th>
<th>性别</th>
<th>课程</th>
<th>操作</th>
</tr>
<tr>
<td>1</td>
<td>张三</td>
<td>男</td>
<td>C语言</td>
<td><a href="#">修改</a> <a href="#">删除</a></td>
</tr>
<tr>
<td>2</td>
<td>李四</td>
<td>女</td>
<td>数据库</td>
<td><a href="#">修改</a> <a href="#">删除</a></td>
</tr>
</table>
</body>
</html>
3.课程新增、课程修改
(1)课程新增
- 新建subjectList.jsp页面
- 编写代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="addSubject">
名称:<input type="text" name="name"><br>
学分:<input type="text" name="credit"><br>
<input type="submit" value="新增">
</form>
</body>
</html>
(2)课程修改
- 新建subjectModify.jsp页面
- 编写代码
<%@page import="edu.wdu.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>
<%
Subject subject = (Subject)session.getAttribute("subject");
%>
<form action="updateSubject">
<input type="hidden" name="id" value="<%=subject.getId() %>">
名称:<input type="text" name="name" value="<%=subject.getName() %>"><br>
学分:<input type="text" name="credit" value="<%=subject.getCredit() %>"><br>
<input type="submit" value="修改">
</form>
</body>
</html>
注:这里代码可能会报错,这是由于我们的完整页面还没有搭建起来,因此报错。 这里暂时先不管,我们先将这部分页面搭建起来。
4.学生添加、学生修改
(1)学生添加
- 新建studentAdd.jsp页面
- 编写代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="#">
姓名:<input type="text" name="name"><br>
性别:
<select name="gender">
<option value='0'>请选择性别</option>
<option value='男'>男</option>
<option value='女'>女</option>
</select><br>
课程:
<select name="subject">
<option value='0'>请选择课程</option>
<option value='1'>C语言</option>
<option value='2'>数据库</option>
</select><br>
<input type="submit" value="新增">
</form>
</body>
</html>
(2)学生修改
- 新建studentModify.jsp页面
- 编写代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="#">
姓名:<input type="text" name="name"><br>
性别:
<select name="gender">
<option value='0'>请选择性别</option>
<option value='男' selected>男</option>
<option value='女'>女</option>
</select><br>
课程:
<select name="subject">
<option value='0'>请选择课程</option>
<option value='1' selected>C语言</option>
<option value='2'>数据库</option>
</select><br>
<input type="submit" value="修改">
</form>
</body>
</html>
三、系统逻辑代码的编写
1.新建文件夹csdn.ah.dao
(1)文件夹下新建SubjectDao.java类
(2)编写代码
package csdn.ah.Dao;
import java.util.ArrayList;
import csdn.ah.pojo.Subject;
public class SubjectDao {
private static ArrayList<Subject> list = new ArrayList<Subject>();
private static int index=0;
public static ArrayList<Subject> subjectList() {
return list;
}
public static void addSubject(String name,int credit) {
index++;
Subject subject = new Subject(index, name, credit);
list.add(subject);
}
public static Subject getById(int id) {
Subject subject = null;
for(Subject sub:list) {
if(sub.getId() == id) {
subject = sub;
break;
}
}
return subject;
}
public static void updateSubject(int id,String name,int credit) {
for(Subject sub:list) {
if(sub.getId() == id) {
sub.setName(name);
sub.setCredit(credit);
break;
}
}
}
public static void deleteSubject(int id) {
Subject sub=null;
for(int i=0;i<list.size();i++) {
sub = list.get(i);
if(sub.getId() == id) {
list.remove(i);
break;
}
}
}
}
注:可能代码会报错,因为我们的全部逻辑代码还未搭建完成 。
2.新建包csdn.ah.pojo
(1)文件夹下新建Subject.java类
(2)编写代码
package csdn.ah.pojo;
public class Subject {
private int id;
private String name;
//学分
private int credit;
public Subject() {
super();
// TODO 自动生成的构造函数存根
}
public Subject(int id, String name, int credit) {
super();
this.id = id;
this.name = name;
this.credit = credit;
}
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 int getCredit() {
return credit;
}
public void setCredit(int credit) {
this.credit = credit;
}
}
3.新建包csdn.ah.servlet
(1)包下新建AddSubjectServlet.java类(servlet类)、DeleteSubjectServlet.java类(servlet类)、SubjectListServlet.java类(servlet类)、ToSubjectModifyServlet.java类(servlet类)、UpdateSubjectServlet.java类(servlet类)
(2)编写代码
- AddSubjectServlet.java
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 AddSubjectServlet
*/
@WebServlet("/addSubject")
public class AddSubjectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddSubjectServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取参数
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String creditStr = request.getParameter("credit");
int credit = Integer.parseInt(creditStr);
//2.新增课程
SubjectDao.addSubject(name, credit);
//3.查询最新课程列表
ArrayList<Subject> list = SubjectDao.subjectList();
HttpSession session = request.getSession();
//4.更新session存储的列表
session.setAttribute("subjectList", list);
//5.重定向课程管理页
response.sendRedirect("subjectList.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);
}
}
- DeleteSubjectServlet.java
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 DeleteSubjectServlet
*/
@WebServlet("/deleteSubject")
public class DeleteSubjectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteSubjectServlet() {
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.删除数据
SubjectDao.deleteSubject(id);
//3.查询最新课程列表
ArrayList<Subject> list = SubjectDao.subjectList();
HttpSession session = request.getSession();
//4.更新session存储的列表
session.setAttribute("subjectList", list);
//5.重定向课程管理页
response.sendRedirect("subjectList.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);
}
}
- SubjectListServlet.java
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 SubejctListServlet
*/
@WebServlet("/subjectList")
public class SubjectListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SubjectListServlet() {
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("subjectList.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);
}
}
- ToSubjectModifyServlet.java
package csdn.ah.servlet;
import java.io.IOException;
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 ToSubjectModifyServlet
*/
@WebServlet("/toSubjectModify")
public class ToSubjectModifyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ToSubjectModifyServlet() {
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);
Subject subject = SubjectDao.getById(id);
HttpSession session = request.getSession();
session.setAttribute("subject", subject);
response.sendRedirect("subjectModify.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);
}
}
- UpdateSubjectServlet.java
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 UpdateSubjectServlet
*/
@WebServlet("/updateSubject")
public class UpdateSubjectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateSubjectServlet() {
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 creditStr = request.getParameter("credit");
int id = Integer.parseInt(idStr);
int credit = Integer.parseInt(creditStr);
//2.修改课程
SubjectDao.updateSubject(id, name, credit);
//3.查询最新课程列表
ArrayList<Subject> list = SubjectDao.subjectList();
HttpSession session = request.getSession();
//4.更新session存储的列表
session.setAttribute("subjectList", list);
//5.重定向课程管理页
response.sendRedirect("subjectList.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.查看代码报错的地方
此时我们可以看到,报错的地方已经不再报错了。如果你的代码还有问题的化,请把报错的地方删掉重写一遍就好了。eclipse需要对代码修改后保存才能刷新数据。
2.运行测试
总结:
本期内容,我们完成了整个项目的骨架搭建,并完成了课程管理部分的代码编写,学长写到这里已经超过了一万三千多字,觉得本期内容有点过多,决定下期再带大家完成剩下的学生管理部分。关注我,获得最新内容。