基于javaweb+mysql的jsp+servlet学生成绩管理系统(管理员、教师、学生)(java+jsp+servlet+javabean+mysql+tomcat)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
管理员:个人信息、课程管理、成绩管理、教师管理、学生管理
教师:个人信息、课程管理、成绩管理、学生信息查看
学生:人个信息管理、成绩查询
管理员
教师
学生
技术框架
JSP Servlet MySQL JDBC Tomcat CSS JavaScript bootstrap jquery
基于javaweb+mysql的JSP+Servlet学生成绩管理系统(管理员、教师、学生)(java+jsp+servlet+javabean+mysql+tomcat)
// List<FileItem> list=upLoad.parseRequest(request);
// 如果fileitem中封装的是普通输入项的数据
for (FileItem item : list) {
if (item.isFormField()) {
String name = item.getFieldName();
/*// 解决普通输入项的数据中文乱码
String value = item.getString("utf-8");
System.out.println(name + "=" + value);*/
} else {
// 吐过fileitem中封装的是上传文件
// 得到上传的文件名称
String fileName = item.getName();
if (fileName == null || fileName.trim().equals("")) {
continue;
}
// 注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,
// 如: c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
// 处理获取到的上传文件的文件名的路径部分,只保留文件名部分
fileName = fileName
.substring(fileName.lastIndexOf("\\") + 1);
// 得到上传文件扩展名
String fileExtName = fileName.substring(fileName
.lastIndexOf(".") + 1);
// 如果需要限制上传的文件类型,那么可以通过文件扩展名来判断文件类型是否合法
//System.out.println("上传文件扩展名是:" + fileExtName);
// 获取item中的上传文件的输入流
InputStream is = item.getInputStream();
// 得到文件保存的名称
String saveFileName = makeFileName(fileName);
// 得到文件保存目录
String realSavePath = makePath(saveFileName, savePath);
// 创建一个文件输出流
FileOutputStream fos = new FileOutputStream(realSavePath
+ "\\" + saveFileName);
// 创建一个缓冲区
byte buffer[] = new byte[1024];
// 判断输入流中的数据是否已经读完的标识
int len = 0;
// 循环将输入流读入到缓冲区中,(len=in.read(buffer))>0就表示in里面还有数据
while ((len = is.read(buffer)) > 0) {
// 使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\"
// + filename)当中
fos.write(buffer, 0, len);
public class ServletaddScore extends HttpServlet{
private Score score;
private ServletFindAllScore servletFindAllScore=new ServletFindAllScore();
private ScoreServiceImpl scoreServiceImpl=new ScoreServiceImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String stuNum = request.getParameter("stuNum");
String stuName = request.getParameter("stuName");
String stuClass = request.getParameter("stuClass");
String courseName = request.getParameter("courseName");
double scoreGrade=Double.parseDouble(request.getParameter("scoreGrade"));
String major = request.getParameter("major");
score=new Score(stuNum,stuName,stuClass,courseName,scoreGrade,major);
Score scor=scoreServiceImpl.selectScoreInfo(score);
if(scor==null){
int rs=scoreServiceImpl.addScore(score);
if(rs>0){
request.setAttribute("msg", "添加成功!!");
servletFindAllScore.doGet(request, response);
// request.getRequestDispatcher("admin/addCourse.jsp").forward(request, response);
}else{
request.setAttribute("msg", "添加失败!!");
request.getRequestDispatcher("admin/addScore.jsp").forward(request, response);
}
}else{
request.setAttribute("msg", "该生的本门课成绩已录入,请重新录入!!");
request.getRequestDispatcher("admin/addScore.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}else{
request.setAttribute("msg", "修改失败!!");
selectOne.doGet(request, response);
//request.getRequestDispatcher("admin/editCourse.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public class ServletRegistersStudent extends HttpServlet{
private User user;
private Student student;
private UserServiceImpl userServiceImpl = new UserServiceImpl();
private StudentServiceImpl studentServiceImpl =new StudentServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String stuNum = request.getParameter("stuNum");
String stuName = request.getParameter("stuName");
String pwd = request.getParameter("password");
String stuSex = request.getParameter("stuSex");
int stuAge = Integer.parseInt(request.getParameter("stuAge"));
if(rs.getPassword().equals(user.getPassword())){
if(rs.getRoleID()==user.getRoleID()){
if(rs.getRoleID()==2){
request.getSession().setAttribute("username", rs.getUserName());
request.getSession().setAttribute("usernum", rs.getUserNum());
request.getRequestDispatcher("admin/admin.jsp").forward(request, response);
}else if(rs.getRoleID()==1){
request.getSession().setAttribute("username", rs.getUserName());
request.getSession().setAttribute("usernum", rs.getUserNum());
request.getRequestDispatcher("teacher/teacher.jsp").forward(request, response);
}else if(rs.getRoleID()==0){
request.getSession().setAttribute("username", rs.getUserName());
request.getSession().setAttribute("usernum", rs.getUserNum());
request.getRequestDispatcher("student/student.jsp").forward(request, response);
}
}else{
request.setAttribute("massage","角色不正确!!");
request.getRequestDispatcher("login/login.jsp").forward(request, response);
}
}else{
request.setAttribute("massage","密码不正确!!");
request.getRequestDispatcher("login/login.jsp").forward(request, response);
}
}else{
request.setAttribute("massage","用户不存在!!");
request.getRequestDispatcher("login/login.jsp").forward(request, response);
}
}else{
request.setAttribute("massage","用户不存在!!");
request.getRequestDispatcher("login/login.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doGet(request,response);
}
}
public class ServletEditStu extends HttpServlet{
private Student student;
private StudentServiceImpl studentServiceImpl=new StudentServiceImpl();
private ServletFindAllStu findStu=new ServletFindAllStu();
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String stuNum=request.getParameter("stuNum");
String stuName = request.getParameter("stuName");
String stuSex = request.getParameter("stuSex");
int stuAge = Integer.parseInt(request.getParameter("stuAge"));
String stuClass = request.getParameter("stuClass");
String major = request.getParameter("major");
String department = request.getParameter("department");
student = new Student(stuNum, stuName, stuSex, stuAge, stuClass, major,
department);
int rs=studentServiceImpl.editStu(student);
if(rs>0){
request.setAttribute("msg", "修改成功!!");
findStu.doGet(request, response);
}else{
request.setAttribute("msg", "修改失败!!");
request.getRequestDispatcher("admin/editStudent.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doGet(request,response);
}
}
department);
int rs=studentServiceImpl.editStu(student);
if(rs>0){
request.setAttribute("msg", "修改成功!!");
findStu.doGet(request, response);
}else{
request.setAttribute("msg", "修改失败!!");
request.getRequestDispatcher("admin/editStudent.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doGet(request,response);
}
}
public class ServletEditTea extends HttpServlet{
private Teacher teacher;
private TeacherServiceImpl teacherServiceImpl=new TeacherServiceImpl();
private ServletFindAllTea findTea=new ServletFindAllTea();
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
@SuppressWarnings("serial")
public class ServletUploadFile extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 得到上传文件的保存目录,将上传的文件存放于服务器WEB-INF目录下,不允许外界直接访问,保证上传文件的安全性
String savePath = this.getServletContext().getRealPath(
"/WEB-INF/upload");
// 上传时生成临时文件保存目录
String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
// 创建临时文件目录
tempFile.mkdir();
}
// 消息提醒
String message = "";
try {
// 使用Apache文件上传组件处理文件上传步骤
// 1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置工厂的缓冲区的大小,当上传的文件大小超过缓冲区的大小时,就会生成一个临时文件存放到指定的临时目录当中。
factory.setSizeThreshold(1024 * 100); // 设置缓冲区的大小为100KB,如果不指定,那么缓冲区的大小默认是10KB
// 设置上传时的临时保存目录
factory.setRepository(tempFile);
// 2,创建一个文件上传解析器
ServletFileUpload upLoad = new ServletFileUpload(factory);
// 解决上传文件中文乱码
upLoad.setHeaderEncoding("utf-8");
// 3、判断提交上来的数据是否是上传表单的数据
if (!ServletFileUpload.isMultipartContent(request)) {
// 按照传统方式获取数据
return;
}
// 设置上传单个文件的大小的最大值,目前是设置为1024*1024字节,也就是1MB
upLoad.setFileSizeMax(1024 * 1024);
// request.getRequestDispatcher("admin/addCourse.jsp").forward(request, response);
}else{
request.setAttribute("msg", "修改失败!!");
request.getRequestDispatcher("admin/editScore.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public class ServletEditStudentInfo extends HttpServlet{
private Student student;
private StudentServiceImpl studentServiceImpl=new StudentServiceImpl();
private ServletSelectStudentInfo findStu=new ServletSelectStudentInfo();
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String stuNum=request.getParameter("stuNum");
String stuName = request.getParameter("stuName");
String stuSex = request.getParameter("stuSex");
int stuAge = Integer.parseInt(request.getParameter("stuAge"));
String stuClass = request.getParameter("stuClass");
String major = request.getParameter("major");
int roleID = Integer.parseInt(request.getParameter("role"));
user = new User(stuNum, stuName, pwd, phone, roleID);
student = new Student(stuNum, stuName, stuSex, stuAge, stuClass, major,
department);
//验证是否存在该学生
User rs=userServiceImpl.login(user);
if(rs.getUserID()==0){
int rsUser=userServiceImpl.addUser(user);
int rsStu=studentServiceImpl.addStu(student);
if(rsStu>0 && rsUser>0){
request.setAttribute("msg", "添加成功!!");
findStu.doGet(request, response);
/*List list=studentDaoImpl.findAll();
request.setAttribute("list", list);
request.getRequestDispatcher("admin/studentAllInfo.jsp").forward(request, response);*/
}else{
request.setAttribute("msg", "添加失败!!");
request.getRequestDispatcher("admin/addStudent.jsp").forward(request, response);
}
}else{
request.setAttribute("msg", "该学号已存在,请重新录入!!");
request.getRequestDispatcher("admin/addStudent.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public class ServletListFile extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
request.getRequestDispatcher("admin/addTeacher.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public class ServletLogin extends HttpServlet{
private User user;
private UserServiceImpl userServiceImpl=new UserServiceImpl();
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String num=request.getParameter("ID");
String pwd=request.getParameter("pwd");
int roleID=Integer.parseInt(request.getParameter("role"));
user=new User(num,pwd,roleID);
User rs=userServiceImpl.login(user);
if(rs.getUserName()!=null){
if(rs.getUserNum().equals(user.getUserNum())){
if(rs.getPassword().equals(user.getPassword())){
if(rs.getRoleID()==user.getRoleID()){
if(rs.getRoleID()==2){
request.getSession().setAttribute("username", rs.getUserName());
request.getSession().setAttribute("usernum", rs.getUserNum());
request.getRequestDispatcher("admin/admin.jsp").forward(request, response);
}else if(rs.getRoleID()==1){
request.getSession().setAttribute("username", rs.getUserName());
request.getSession().setAttribute("usernum", rs.getUserNum());
request.getRequestDispatcher("teacher/teacher.jsp").forward(request, response);
}else if(rs.getRoleID()==0){
request.getSession().setAttribute("username", rs.getUserName());
throws ServletException, IOException {
/*request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");*/
//得到要下载的文件名
String fileName=request.getParameter("fileName");
fileName=new String(fileName.getBytes("iso8859-1"),"utf-8");
//上传的文件都保存在/WEB—INF/upload目录下的子目录当中
String fileSaveRootPath=this.getServletContext().getRealPath("/WEB-INF/upload");
//通过文件名找出文件所在目录
String path=findFileSavePathByFileName(fileName, fileSaveRootPath);
//得到要下载的文件
File file=new File(path+"\\"+fileName);
//如果文件不存在
if(!file.exists()){
request.setAttribute("message", "文件不存在!!!");
request.getRequestDispatcher("admin/fileLoadFail.jsp").forward(request, response);
return ;
}
/*
String realname = fileName.substring(fileName.indexOf("_")+1);
//设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
*/
//处理文件名
String realName=fileName.substring(fileName.indexOf("_")+1);
if ("FF".equals(getBrowser(request))) {
// 针对火狐浏览器处理方式不一样了
realName = new String(realName.getBytes("UTF-8"),
"iso-8859-1") ;
}
else{
realName=URLEncoder.encode(realName, "utf-8");
}
//设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;fileName="+realName);
//读取要下载的文件,保存到文件输入流
FileInputStream fis=new FileInputStream(path+"\\"+fileName);
//创建输出流
OutputStream os=response.getOutputStream();
//创建缓冲区
byte buffer[]=new byte[1024];
int len=0;
//循环将输入流中的内容读取到缓冲区当中
while((len=fis.read(buffer))>0){
os.write(buffer, 0, len);
}
fis.close();
os.close();
String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
// 创建临时文件目录
tempFile.mkdir();
}
// 消息提醒
String message = "";
try {
// 使用Apache文件上传组件处理文件上传步骤
// 1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置工厂的缓冲区的大小,当上传的文件大小超过缓冲区的大小时,就会生成一个临时文件存放到指定的临时目录当中。
factory.setSizeThreshold(1024 * 100); // 设置缓冲区的大小为100KB,如果不指定,那么缓冲区的大小默认是10KB
// 设置上传时的临时保存目录
factory.setRepository(tempFile);
// 2,创建一个文件上传解析器
ServletFileUpload upLoad = new ServletFileUpload(factory);
// 解决上传文件中文乱码
upLoad.setHeaderEncoding("utf-8");
// 3、判断提交上来的数据是否是上传表单的数据
if (!ServletFileUpload.isMultipartContent(request)) {
// 按照传统方式获取数据
return;
}
// 设置上传单个文件的大小的最大值,目前是设置为1024*1024字节,也就是1MB
upLoad.setFileSizeMax(1024 * 1024);
// 设置上传文件总量的最大值,最大值=同时上传的多个文件的大小的最大值的和,目前设置为10MB
upLoad.setSizeMax(1024 * 1024 * 10);
// 4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,
// 每一个FileItem对应一个Form表单的输入项
List<FileItem> list = upLoad.parseRequest(request);
// List<FileItem> list=upLoad.parseRequest(request);
// 如果fileitem中封装的是普通输入项的数据
for (FileItem item : list) {
if (item.isFormField()) {
String name = item.getFieldName();
/*// 解决普通输入项的数据中文乱码
String value = item.getString("utf-8");
if(pageNoStr!=null){
pageNo=Integer.parseInt(pageNoStr);
}
if(pageCountStr!=null){
pageCount=Integer.parseInt(pageCountStr);
}
String stuNum=request.getParameter("stuNum");
String stuName=request.getParameter("stuName");
String stuClass=request.getParameter("stuClass");
score=new Score();
score.setStuNum(stuNum);
score.setStuName(stuName);
score.setStuClass(stuClass);
PageBean list=scoreServiceImpl.selectTeaScore(pageNo, pageCount, teaName,score);
request.setAttribute("list", list);
request.setAttribute("score", score);
request.getRequestDispatcher("teacher/stuTeaScore.jsp").forward(request, response);
}
}
public class ServletEditStu extends HttpServlet{
private Student student;
private StudentServiceImpl studentServiceImpl=new StudentServiceImpl();
private ServletFindAllStu findStu=new ServletFindAllStu();
public void doGet(HttpServletRequest request,HttpServletResponse response)
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取上传文件的目录
String uploadFilePath=this.getServletContext().getRealPath("/WEB-INF/upload");
//存储要下载的文件名
Map<String,String> fileNameMap=new HashMap<String, String>();
/*
* 递归遍历filepath目录下的所有文件和目录,将文件的文件名存储到map集合中
*/
//file既可以代表一个文件也可以代表一个目录
listFile(new File(uploadFilePath),fileNameMap);
//将map集合发送到listFile.jsp页面进行显示
request.setAttribute("fileNameMap", fileNameMap);
request.getRequestDispatcher("admin/fileList.jsp").forward(request, response);
}
private void listFile(File file, Map<String, String> fileNameMap) {
//如果file代表的不是一个文件,而是一个目录
if(!file.isFile()){
//列出该目录下的所有文件和目录
File files[]=file.listFiles();
//遍历files数组
for(File f:files){
//递归
listFile(f,fileNameMap);
}
}else{
/**
* 处理文件名,上传后的文件是以uuid_文件名的形式去重新命名的,去除文件名的uuid_部分
file.getName().indexOf("_")检索字符串中第一次出现"_"字符的位置,
如果文件名类似于:9349249849-88343-8344_阿_凡_达.avi
那么file.getName().substring(file.getName().indexOf("_")+1)
处理之后就可以得到阿_凡_达.avi部分
*/
if(courseName==""){
courseName=null;
}
if(stuName==""){
stuName=null;
}
if(stuClass==""){
stuClass=null;
}
if(major==""){
major=null;
}*/
double scoreGrade=0;
score=new Score( stuNum, stuName, stuClass,
courseName, scoreGrade, major);
PageBean rs=scoreServiceImpl.selectStuScore(pageNo, pageCount, score);
request.setAttribute("list", rs);
request.setAttribute("score", score);
request.getRequestDispatcher("admin/selectblurScore.jsp").forward(request, response);
}
}
public class ServletEditUser extends HttpServlet{
private User user;
private ServletFindAllUser servletFindAllUser=new ServletFindAllUser();
private UserServiceImpl userServiceImpl=new UserServiceImpl();
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String userNum=request.getParameter("userNum");
String userName=request.getParameter("userName");
String pwd=request.getParameter("password");
String phone=request.getParameter("phone");
int roleID=Integer.parseInt(request.getParameter("roleID"));
user=new User(userNum,userName,pwd,phone,roleID);
int rs=userServiceImpl.editUser(user);
if(rs>0){
}
return null;
}
}
public class ServletaddTeacher extends HttpServlet{
private User user;
private Teacher teacher;
private UserServiceImpl userServiceImpl = new UserServiceImpl();
private TeacherServiceImpl teacherServiceImpl =new TeacherServiceImpl();
private ServletFindAllTea findTea=new ServletFindAllTea();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String teaNum = request.getParameter("teaNum");
String teaName = request.getParameter("teaName");
String pwd = request.getParameter("password");
String teaSex = request.getParameter("teaSex");
int teaAge = Integer.parseInt(request.getParameter("teaAge"));
String teaCourse = request.getParameter("teaCourse");
String major = request.getParameter("major");
String department = request.getParameter("department");
String phone = request.getParameter("phone");
int roleID = Integer.parseInt(request.getParameter("role"));
user = new User(teaNum, teaName, pwd, phone, roleID);
teacher = new Teacher(teaNum, teaName, teaSex, teaAge, teaCourse, major,
department);
//验证是否存在该教师
User rs=userServiceImpl.login(user);
request.setAttribute("msg", "修改失败!!");
//servletFindAllUser.doGet(request, response);
// request.getRequestDispatcher("admin/selectPwd.jsp").forward(request, response);
}
new PageServlet().doGet(request,response);
/*int userID=Integer.parseInt(request.getParameter("userID"));
List rs=userDaoImpl.selectOneUser(userID);
request.setAttribute("list", rs);
request.getRequestDispatcher("admin/editUser.jsp").forward(request, response);*/
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doGet(request,response);
}
}
public class ServletEditScore extends HttpServlet{
private Score score;
private ServletFindAllScore servletFindAllScore=new ServletFindAllScore();
private ScoreServiceImpl scoreServiceImpl=new ScoreServiceImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int scoreID = Integer.parseInt(request.getParameter("scoreID"));