基于Servlet+JSP的学生信息管理系统的设计与实现(免费源码放评论区了)
一、项目名称二、项目环境1、软件环境:2、项目思路 :3、项目所用知识点:4、项目架构:三、项目主要源码1.数据库设计1.1 user用户表1.2 admin管理员表1.3 student_info学生信息表2.前端界面设计2.1 登录界面2.2 用户界面2.3 管理员界面3.后端Servlet设计3.1 LoginServlet3.2 StudentManageServlet3.3 AdminS
·
目录
一、项目名称
使用MVC模式和三层开发架构,开发一个完整的学生信息管理系统。
二、项目环境
1、软件环境:
系统环境:win10
JDK版本: jdk1.8.0_182
服务器版本:apache-tomcat-8.5.89
2、项目思路 :
通过MVC开发流程,实现项目的架构。然后利用Servlet+Jsp+Jdbc的功能开发流程,实现登陆注册退出功能。
通过Servlet实现服务器在接收到浏览器发送的请求后,会调用对应的Servlet进行请求处理,从而将多个业务逻辑功能组合到一起。
3、项目所用知识点:
bootstrap、font-awesome、datetimepicker前端框架的调用
MVC开发流程md5对密码加密
ajax实现登录
Servlet+Jsp+Jdbc的功能开发流程
request和session作用域的使用
浏览器发起请求到服务器的方式
4、项目架构:
三、项目主要源码
1.数据库设计
1.1 user用户表
-- auto-generated definition
create table user
(
id int auto_increment
primary key,
username varchar(64) null,
password varchar(64) null,
name varchar(64) null,
telephone bigint null,
email varchar(64) null,
gender varchar(64) null,
age int null
);
1.2 admin管理员表
-- auto-generated definition
create table admin
(
id int auto_increment
primary key,
username varchar(64) null,
password varchar(64) null,
name varchar(64) null,
telephone bigint null,
email varchar(64) null,
gender varchar(64) null,
age int null
);
1.3 student_info学生信息表
-- auto-generated definition
create table student_info
(
id int auto_increment
primary key,
name varchar(50) null,
age int null,
classes varchar(50) null,
studentId varchar(20) null,
gender varchar(10) null,
phone varchar(20) null,
email varchar(50) null,
admissionDate date null
);
2.前端界面设计
2.1 登录界面
<%@ page language="java" pageEncoding="utf-8" %>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta content="ie=edge">
<title>Title</title>
<script src="js/jquery-3.6.0.min.js"></script>
<link href="css/bootstrap3.3.7.css" rel="stylesheet">
</head>
<body>
<script src="js/jquery-3.6.0.min.js"></script>
<script>
$(function () {
const submitBtn = $("#submit");
const rememberBtn=$("#remember");
const usernameInput = $("#username");
const passwordInput = $("#password");
const typeInput = $("#type");
submitBtn.click(function () {
const username = usernameInput.val().trim();
const password = passwordInput.val().trim();
const type = typeInput.val().trim();
if (!username || !password) {
alert("用户名和密码不能为空!");
return;
}
$.ajax({
url: "/account?method=login",
type: "POST",
data: {
username,
password,
type,
remember: rememberBtn.is(":checked") ? "true" : "false"
},
dataType: "json",
success: function ({success1, success2, message}) {
if (success1) {
window.location.assign("/usersystem.jsp");
} else if (success2) {
window.location.assign("/adminsystem.jsp");
} else {
alert(message);
}
},
error: function () {
alert("登录失败!");
}
});
});
});
</script>
<form class="form-horizontal" style="background-color: #f2f2f2; padding: 20px;">
<div class="form-group">
<label class="control-label col-sm-2" for="username" style="font-size: 18px;">用户名:</label>
<div class="col-sm-2">
<input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名"
style="padding: 5px; border-radius: 5px; border: none; box-shadow: 1px 1px 2px #888888;">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="password" style="font-size: 18px;">密码:</label>
<div class="col-sm-2">
<input type="password" class="form-control" id="password" name="password" placeholder="请输入密码"
style="padding: 5px; border-radius: 5px; border: none; box-shadow: 1px 1px 2px #888888;">
</div>
</div>
<
<div class="form-group">
<label class="control-label col-sm-2" for="type" style="font-size: 18px;">用户类型:</label>
<div class="col-sm-2">
<select class="form-control" id="type" name="type"
style="padding: 5px; border-radius: 5px; border: none; box-shadow: 1px 1px 2px #888888;">
<option value="user">用户</option>
<option value="admin">管理员</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success" id="submit"
style="padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; margin-right: 10px;">
登录
</button>
<button type="reset" class="btn btn-success"
style="padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; margin-right: 10px;">
重置
</button>
<input type="checkbox" name="remember" id="remember">
<label for="remember">记住我</label>
</div>
</div>
</form>
</body>
</html>
2.2 用户界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户首界面</title>
<!-- 引入 Bootstrap -->
<script src="js/jquery-3.6.0.min.js"></script>
<link href="css/bootstrap3.3.7.css" rel="stylesheet">
<!-- 引入 font-awesome -->
<link href="css/font-awesome.css" rel="stylesheet">
<script src="js/bootstrap3.3.7.js"></script>
<script type="application/javascript">
function change(url, index) {
$(".list-group-item").removeClass("active");
$(".list-group-item").eq(index).addClass("active");
$("iframe").attr("src", url);
}
</script>
</head>
<body>
<nav class="navbar navbar-inverse" role="navigation">
<div class="container-fluid">
<ul class="nav navbar-nav navbar-left">
<li>
<a style="font-size: 26px;">学生信息管理系统</a>
</li>
</ul>
<span style="color: #CCCCCC;font-size: 26px;position: relative;top: 5px;"></span>
<ul class="nav navbar-nav navbar-right">
<li>
<a>实时在线人数:${onlineCount-1}</a>
</li>
<li>
<a>欢迎您,${username}</a>
</li>
<li>
<a href="/account?method=logout">安全退出</a>
</li>
</ul>
</div>
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">
<a href="javascript:void(0)" class="list-group-item active"
onclick="change('/sm?method=list',0)">
<span class="" aria-hidden="true">
<i class="fa fa-user-circle-o fa-fw"></i>
</span>学生信息管理
</a>
</div>
<!--右边内容-->
<iframe style="width: 81%; height: 600px; border: 0px;" src="/sm?method=list"></iframe>
</div>
</div>
<div class="footer">
<p class="text-center">
© 2023 学生信息管理系统。保留所有权利。未经许可,不得复制或转载。
</p>
</div>
</body>
</html>
2.3 管理员界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- 引入 Bootstrap -->
<script src="js/jquery-3.6.0.min.js"></script>
<link href="css/bootstrap3.3.7.css" rel="stylesheet">
<!-- 引入 font-awesome -->
<link href="css/font-awesome.css" rel="stylesheet">
<script src="js/bootstrap3.3.7.js"></script>
<script type="application/javascript">
function change(url, index) {
$(".list-group-item").removeClass("active");
$(".list-group-item").eq(index).addClass("active");
$("iframe").attr("src", url);
}
</script>
</head>
<body>
<nav class="navbar navbar-inverse" role="navigation">
<div class="container-fluid">
<ul class="nav navbar-nav navbar-left">
<li>
<a style="font-size: 26px;">学生信息管理系统</a>
</li>
</ul>
<span style="color: #CCCCCC;font-size: 26px;position: relative;top: 5px;"></span>
<ul class="nav navbar-nav navbar-right">
<li>
<a>实时在线人数:${onlineCount-1}</a>
</li>
<li>
<a>欢迎您,${username}</a>
</li>
<li>
<a href="/account?method=logout">安全退出</a>
</li>
</ul>
</div>
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">
<a href="javascript:void(0)" class="list-group-item active" onclick="change('/admin?method=list',0)">
<span class="" aria-hidden="true">
<i class="fa fa-bookmark fa-fw"></i>
</span>查询用户信息
</a>
<a href="javascript:void(0)" class="list-group-item" onclick="change('#',1)">
<span class="" aria-hidden="true">
<i class="fa fa-bookmark-o fa-fw"></i>
</span>更多
</a>
</div>
<!--右边内容-->
<iframe style="width: 81%; height: 600px; border: 0px;" src="/admin?method=list"></iframe>
</div>
</div>
<div class="footer">
<p class="text-center">
© 2023 学生信息管理系统。保留所有权利。未经许可,不得复制或转载。
</p>
</div>
</body>
</html>
3.后端Servlet设计
3.1 LoginServlet
package com.hhb.web.Servlet;
import com.alibaba.fastjson.JSONObject;
import com.hhb.service.AdminService;
import com.hhb.service.UserService;
import com.hhb.service.impl.AdminServiceImpl;
import com.hhb.service.impl.UserServiceImpl;
import com.hhb.utils.MD5Util;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/account")
public class LoginServlet extends HttpServlet {
private UserService userService = new UserServiceImpl();
private AdminService adminService = new AdminServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
String username = request.getParameter("username");
String password = request.getParameter("password");
String type = request.getParameter("type");
boolean rem = request.getParameter("remember") != null;
request.setCharacterEncoding("UTF-8");
switch (method) {
case "login":
JSONObject result = new JSONObject();
password = MD5Util.md5(password);
switch (type) {
case "user":
if (userService.getUser(username, password) && "user".equals(type)) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
if (rem) {
session.setMaxInactiveInterval(300);
}
result.put("success1", true);
} else {
result.put("success1", false);
result.put("message", "用户名或密码错误!");
}
break;
case "admin":
if (adminService.getAdmin(username, password) && "admin".equals(type)) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
result.put("success2", true);
} else {
result.put("success2", false);
result.put("message", "用户名或密码错误!");
}
break;
}
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(result.toString());
break;
case "logout":
request.getSession().invalidate();
response.sendRedirect("/login.jsp");
break;
}
}
}
3.2 StudentManageServlet
package com.hhb.web.Servlet;
import com.hhb.entity.StudentInfo;
import com.hhb.service.StudentManageService;
import com.hhb.service.impl.StudentManageServiceImpl;
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 java.io.IOException;
import java.sql.Date;
@WebServlet("/sm")
public class StudentManageServlet extends HttpServlet {
private StudentManageService studentManageService = new StudentManageServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
request.setCharacterEncoding("UTF-8");
switch (method) {
case "list":
request.setAttribute("list", this.studentManageService.list());
request.getRequestDispatcher("studentmanage.jsp").forward(request, response);
break;
case "search":
String key = request.getParameter("key");
String value = request.getParameter("value");
value = new String(value.getBytes("ISO8859-1"), "UTF-8");
request.setAttribute("list", this.studentManageService.search(key, value));
request.getRequestDispatcher("studentmanage.jsp").forward(request, response);
break;
case "delete":
String studentId = request.getParameter("studentId");
this.studentManageService.delete(studentId);
response.sendRedirect("/sm?method=list");
break;
case "save":
String name = request.getParameter("name");
name = new String(name.getBytes("ISO8859-1"), "UTF-8");
String ageStr = request.getParameter("age");
int age = Integer.parseInt(ageStr);
String classes = request.getParameter("classes");
studentId = request.getParameter("studentId");
String gender = request.getParameter("gender");
gender = new String(gender.getBytes("ISO8859-1"), "UTF-8");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String admissionDateStr = request.getParameter("admissionDate");
Date admissionDate = Date.valueOf(admissionDateStr);
this.studentManageService.save(new StudentInfo(name, age, classes, studentId, gender, phone, email, admissionDate));
response.sendRedirect("/sm?method=list");
break;
case "update":
name = request.getParameter("name");
name = new String(name.getBytes("ISO8859-1"), "UTF-8");
ageStr = request.getParameter("age");
age = Integer.parseInt(ageStr);
classes = request.getParameter("classes");
classes = new String(classes.getBytes("ISO8859-1"), "UTF-8");
studentId = request.getParameter("studentId");
gender = request.getParameter("gender");
gender = new String(gender.getBytes("ISO8859-1"), "UTF-8");
phone = request.getParameter("phone");
email = request.getParameter("email");
this.studentManageService.update(new StudentInfo(name, age, classes, gender, phone, email, studentId));
response.sendRedirect("/sm?method=list");
break;
}
}
}
3.3 AdminServlet
package com.hhb.web.Servlet;
import com.hhb.entity.User;
import com.hhb.service.AdminService;
import com.hhb.service.StudentManageService;
import com.hhb.service.impl.AdminServiceImpl;
import com.hhb.service.impl.StudentManageServiceImpl;
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 java.io.IOException;
@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
private StudentManageService studentManageService = new StudentManageServiceImpl();
private AdminService adminService = new AdminServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
request.setCharacterEncoding("UTF-8");
switch (method) {
case "list":
request.setAttribute("list", this.adminService.list());
request.getRequestDispatcher("usermessage.jsp").forward(request, response);
break;
case "search":
String key = request.getParameter("key");
String value = request.getParameter("value");
value = new String(value.getBytes("ISO8859-1"), "UTF-8");
request.setAttribute("list", this.adminService.search(key, value));
request.getRequestDispatcher("usermessage.jsp").forward(request, response);
break;
case "delete":
String idStr = request.getParameter("id");
int id = Integer.parseInt(idStr);
this.adminService.delete(id);
response.sendRedirect("/admin?method=list");
break;
case "save":
idStr = request.getParameter("id");
id = Integer.parseInt(idStr);
String username = request.getParameter("username");
String password = request.getParameter("password");
String name = request.getParameter("name");
name = new String(name.getBytes("ISO8859-1"), "UTF-8");
String ageStr = request.getParameter("age");
int age = Integer.parseInt(ageStr);
this.adminService.save(new User(id, username, password, name, age));
response.sendRedirect("/admin?method=list");
break;
case "update":
idStr = request.getParameter("id");
id = Integer.parseInt(idStr);
username = request.getParameter("username");
password = request.getParameter("password");
name = request.getParameter("name");
name = new String(name.getBytes("ISO8859-1"), "UTF-8");
ageStr = request.getParameter("age");
age = Integer.parseInt(ageStr);
this.adminService.update(new User(id, username, password, name, age));
response.sendRedirect("/admin?method=list");
break;
}
}
}
四、项目展示
更多推荐
已为社区贡献2条内容
所有评论(0)