Java项目:JSP+Servlet在线考试管理系统

简介:Java领域优质创作者、Java项目、学习资料、技术互助

文末获取源码

项目介绍

本项目包含管理员与学生两个角色;
管理员角色包含以下功能:
登录页面,管理员首页,考生信息管理,成绩管理,考生录入,题库管理,题目录入,试卷管理等功能。

学生角色包含以下功能:
学生登录,学生主页,开始考试,查看成绩等功能。

由于本程序规模不大,可供课程设计,毕业设计学习演示之用

PS:在添加学生或者添加试题的时候,可以上传文件来实现,文件的模板在管理试题和管理学生的时候,点击下载就有了。

环境需要

1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.数据库:MySql 5.7版本;

6.是否Maven项目:否;

技术栈

1. 后端:Servlet

2. 前端:JSP+jQuery

使用说明

1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中配置文件中的数据库配置改为自己的配置;

4. 运行项目,输入localhost:8080/ 登录

运行截图

管理员登录

学生角色

相关代码

BatchAdditionServlet

package examservlet.control; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; import examService.service.QuestionExcelService; import examService.service.StuInfoExcelService; import exambean.model.QuestionBean; import exambean.model.StudentInfoBean; import examdao.model.DatabassAccessObject; /** * @author matou * 该类接受用户的读取Excel文件请求; * 调用ExcelService类解析Excel文件; * 将解析的表单信息写入数据库表中; * 此外,还可以接受批量上传的图片,并保存到工程目录下 */ @WebServlet("/HandleBatchAdd") @MultipartConfig // 支持文件上传 public class BatchAdditionServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); // 编码处理 response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String excelSorcePath = ""; Integer mode = Integer.valueOf(request.getParameter("mode")); String savePath = getServletContext().getRealPath("/images"); /** * 以下部分为获取上传Excel文件并保存到发布目录 */ Part part = request.getPart("excel"); // Excle文件 String fileName = part.getSubmittedFileName(); // 获取part对象所携带的文件名称 if (fileName.length() > 0) { // 如果文件不为空 savePath = getServletContext().getRealPath("/images"); part.write(savePath + "/" + fileName);// 上传文件到发布目录下 excelSorcePath = savePath + "/" + fileName; // 图片的目标路径(当前项目目录) } /** * 以下部分为获取批量上传的配图 */ for (Part imgPath : request.getParts()) { // 遍历上传的每一个part if (imgPath.getName().startsWith("img")) { // 上传按钮也是part对象,在这里用if条件语句过滤它 // 只接受part名称标头为"img"的正文图片数据 String fileName1 = imgPath.getSubmittedFileName(); // 获取单张图片的文件名 if (fileName1 == null || fileName1.length() == 0) break; try { imgPath.write(savePath + "/" + fileName1); // 写入图片到tomcat的发布目录下 } catch (Exception e) { System.out.println(e); } /** * 接下来要把图片从【tomcat发布目录】拷贝到【当前项目目录】 原因:发布目录在tomcat重启会重新发布,上传的图片需要迁移到永久保留的位置 * 目的:把图片拷贝到项目目录,上传的图片得以保留,可以任意次数发布 */ try {// try能保证流被正确关闭 Path sorcePath = Paths.get(savePath + "/" + fileName1); // 图片的原始路径(tomcat的发布目录下) // 图片的的目标路径(当前项目目录) Path targetPath = Paths.get("E:\\WorkSpace_All\\cli_workspace\\" + request.getContextPath() + "/WebRoot/images/" + fileName1); Files.copy(sorcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); // 复制文件,并替换已存在的文件 } catch (Exception e) { } } } try { DatabassAccessObject db; switch (mode) { case 1: List<QuestionBean> listExcel = QuestionExcelService.getAllByExcel(excelSorcePath); db = new DatabassAccessObject(); for (QuestionBean queBean : listExcel) { if (!QuestionExcelService.isExist(queBean.getQ_id())) { db.insert("insert into question values (?,?,?,?,?,?,?) ; ", queBean.getQ_id(), queBean.getQ_type(), queBean.getQ_title(), queBean.getQ_select(), queBean.getQ_score(), queBean.getQ_key(), queBean.getQ_img()); } else { db.modify( "update question set type = ? , title = ? , `select` = ? , score = ? , `key` = ? , img = ? where number = ? ;", queBean.getQ_type(), queBean.getQ_title(), queBean.getQ_select(), queBean.getQ_score(), queBean.getQ_key(), queBean.getQ_img(), queBean.getQ_id()); } } System.out.println("数据导入成功"); PrintWriter out = response.getWriter(); out.println("<script language=javascript>alert('数据导入成功');window.location='" + request.getContextPath() + "/ShowQuePage';</script>"); break; case 2: List<StudentInfoBean> stuListExcel = StuInfoExcelService.getAllByExcel(excelSorcePath); db = new DatabassAccessObject(); for (StudentInfoBean stuBean : stuListExcel) { if (!StuInfoExcelService.isExist(stuBean.getID())) { db.insert("INSERT INTO student (ID,password,name,class,score) VALUES (?,?,?,?,?) ; ", stuBean.getID(), stuBean.getPassword(), stuBean.getName(), stuBean.getCLASS(), stuBean.getScore()); } else { db.modify("UPDATE student SET password = ? , name = ? , class = ? , score = ? WHERE ID = ? ;", stuBean.getPassword(), stuBean.getName(), stuBean.getCLASS(), stuBean.getScore(), stuBean.getID()); } } System.out.println("数据导入成功"); PrintWriter out2 = response.getWriter(); out2.println("<script language=javascript>alert('数据导入成功');window.location='" + request.getContextPath() + "/ShowStuPage';</script>"); default: break; } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } 

BatchOutServlet

package examservlet.control; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import examService.service.QuestionExcelService; import examService.service.ScoreExcleService; import examService.service.StuInfoExcelService; @WebServlet("/BatchOutServlet") public class BatchOutServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); Integer mode = Integer.valueOf(request.getParameter("mode")); HSSFWorkbook workbook=null; String createExcelname = ""; switch (mode) { case 1: workbook=(HSSFWorkbook) StuInfoExcelService.getAllByDatabase(); createExcelname = "tb_student.xls"; break; case 2: workbook=(HSSFWorkbook) QuestionExcelService.getAllByDatabase(); createExcelname = "tb_question.xls"; break; case 3: workbook=(HSSFWorkbook) ScoreExcleService.getAllByDatabase(); createExcelname = "tb_student_score.xls"; break; default: break; } String dir = request.getSession().getServletContext().getRealPath("/images"); File fileLocation = new File(dir); if (!fileLocation.exists()) { boolean isCreated = fileLocation.mkdir(); } String webUrl = request.getSession().getServletContext().getRealPath("/images"); String outputFile = webUrl + File.separator + createExcelname; FileOutputStream fop = new FileOutputStream(outputFile); workbook.write(fop); fop.flush(); fop.close(); File f = new File(outputFile); if (f.exists() && f.isFile()) { try { FileInputStream fis = new FileInputStream(f); URLEncoder.encode(f.getName(), "utf-8"); byte[] b = new byte[fis.available()]; fis.read(b); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment; filename=" + createExcelname + ""); ServletOutputStream out = response.getOutputStream(); out.write(b); out.flush(); out.close(); if (fis != null) { fis.close(); } f.delete(); } catch (Exception e) { e.printStackTrace(); } } } } 

LoginServlet

package examservlet.control; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletConfig; 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 exambean.model.LoginBean; /** * @author matou * 该类负责处理登录操作 * 匹配账号密码是否对应 * 将登录信息存储到loginBean */ @WebServlet("/HandleLogin") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init(ServletConfig config) throws ServletException { super.init(config); try { Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Connection con; Statement sql; String ID = request.getParameter("ID").trim(); String password = request.getParameter("password").trim(); String userRole = request.getParameter("role").trim(); String uri = "jdbc:mysql://127.0.0.1:3306/jsp_examonline?user=root&password=root&serverTimezone=UTC&characterEncoding=UTF-8"; boolean boo = (ID.length() > 0) && (password.length() > 0); try { con = DriverManager.getConnection(uri); String condition = "select * from " + userRole + " where ID = '" + ID + "' and password ='" + password + "'"; sql = con.createStatement(); if (boo) { ResultSet rs = sql.executeQuery(condition); boolean isExist = rs.next(); if (isExist == true) { success(request, response, rs, userRole); response.sendRedirect(userRole + "/" + userRole + ".jsp"); } else { String backNews = "您输入的学号不存在/密码不匹配"; fail(request, response, ID, backNews); } } else { String backNews = "请输入用户名和密码"; fail(request, response, ID, backNews); } con.close(); } catch (SQLException exp) { String backNews = "" + exp; fail(request, response, ID, backNews); } } /** * 如果账号密码匹配成功,就把登录信息存储到Bean, * 然后重定向进入与角色相相应的管理页面 * @param request 请求对象 * @param response 响应对象 * @param rs 查询结果数据集 * @param role 用户角色 */ private void success(HttpServletRequest request, HttpServletResponse response, ResultSet rs, String role) { HttpSession session = request.getSession(true); LoginBean loginBean = new LoginBean(); try { rs.first(); loginBean.setID(rs.getString("ID")); loginBean.setPassword(rs.getString("password")); loginBean.setName(rs.getString("name")); loginBean.setCLASS(rs.getString("class")); loginBean.setRole(role); if (role.equals("student")) { loginBean.setScore(rs.getFloat("score")); } else { loginBean.setJob(rs.getString("job")); } } catch (SQLException e) { e.printStackTrace(); } session.setAttribute("loginBean", loginBean); } /** * 如果账号密码匹配失败,就重定向回登录页面 * @param request 请求对象 * @param response 响应对象 * @param ID 用户ID * @param backNews 登录失败的原因 */ private void fail(HttpServletRequest request, HttpServletResponse response, String ID, String backNews) { response.setContentType("text/html;charset=UTF-8"); try { PrintWriter out = response.getWriter(); out.println("<script language=javascript>alert('" + backNews + "');window.location='" + request.getContextPath() + "/login.jsp';</script>"); } catch (IOException exp) { } } } 

如果也想学习本系统,下面领取。关注并回复:013jsp

原文链接:https://blog.csdn.net/m0_74967853/article/details/128008874?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171910925216800182172392%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=171910925216800182172392&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-25-128008874-null-null.nonecase&utm_term=2024%E9%AB%98%E8%80%83%E6%9F%A5%E5%88%86

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享