基于javaweb+mysql的jsp+servlet宿舍管理系统(超级管理员、宿舍管理员、学生)
作者:m0_71100782
基于javaweb+mysql的jsp+servlet宿舍管理系统(超级管理员、宿舍管理员、学生)(java+jsp+servlet+javabean+mysql+tomcat),需要的朋友可以参考下
基于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
基于javaweb+mysql的JSP+Servlet宿舍管理系统(超级管理员、宿舍管理员、学生)(java+jsp+servlet+javabean+mysql+tomcat)
httpServletRequest.getSession().setAttribute("session_user", user); chain.doFilter(httpServletRequest, response); }else if(requestUrI.startsWith(path+"/student.action") && !roleId.equals(2)) { //当用户发送的请求是学生模块的请求时,只有当前用户角色不是学生就放行 httpServletRequest.getSession().setAttribute("session_user", user); chain.doFilter(httpServletRequest, response); }else if(requestUrI.startsWith(path+"/record.action") || requestUrI.startsWith(path+"/password.action")|| requestUrI.startsWith(path+"/loginOut.action")|| requestUrI.startsWith(path+"/index.action")) { //当用户发送的请求是考勤、修改密码、退出模块的请求时,不管用户的角色是什么,都放行 httpServletRequest.getSession().setAttribute("session_user", user); chain.doFilter(httpServletRequest, response); }else { httpServletRequest.getRequestDispatcher("/index.jsp").forward(httpServletRequest, response); } } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } } /** * Servlet implementation class LoginServlet */ @WebServlet("/login") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L;
String keyword = request.getParameter("keyword"); //当前要查询的页面 String pageIndex = request.getParameter("pageIndex"); System.out.println("dormBuildId:"+dormBuildId+" searchType:"+searchType+" keyword:"+keyword); //默认查询第一页,需两个参数,当前页码pageIndex,每页展示的条数 PageModel pageModel = new PageModel(); if(pageIndex != null && !pageIndex.equals("")) { pageModel.setPageIndex(Integer.parseInt(pageIndex)); } List<User> students = userService.findStudent(dormBuildId,searchType,keyword,user,pageModel); //获取查询处理的总数量 Integer totalNum = userService.findTotalNum(dormBuildId,searchType,keyword,user); System.out.println("totalNum:"+totalNum); System.out.println("students:"+students); request.setAttribute("totalNum", totalNum); request.setAttribute("pageIndex", pageModel.getPageIndex()); request.setAttribute("dormBuildId", dormBuildId); request.setAttribute("searchType", searchType); request.setAttribute("keyword", keyword); request.setAttribute("students", students); request.setAttribute("mainRight", "/WEB-INF/jsp/studentList.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else if(action != null & action.equals("preAdd")) { //根据用户角色查询宿舍楼进行添加学生, //如当前用户 为宿舍管理员,他只能添加学生到其管理的宿舍楼 //跳转到学生的添加页面 request.setAttribute("mainRight", "/WEB-INF/jsp/studentAddOrUpdate.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else if(action != null & action.equals("save")) { //保存学生 String stuCode = request.getParameter("stuCode"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String tel = request.getParameter("tel"); String passWord = request.getParameter("passWord"); String dormBuildId= request.getParameter("dormBuildId"); String dormCode = request.getParameter("dormCode"); System.out.println("stuCode:"+stuCode+" name:"+name+" sex:"+sex+ " tel:"+tel+" passWord:"+passWord+" dormBuildId:"+dormBuildId+ " dormCode:"+dormCode);
} }else { //保存 if(dormBuild != null) { //当前用户输入的宿舍楼名已存在 //表示跳转到宿舍楼添加页面 request.setAttribute("error", "当前宿舍楼名已存在,请重新输入!"); request.setAttribute("mainRight", "/WEB-INF/jsp/dormBuildAddOrUpdate.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else { //当前用户输入的宿舍楼名不存在,则保存用户输入的信息到数据库 DormBuild build = new DormBuild(); build.setName(name); build.setRemark(remark); build.setDisabled(0); dormBuildService.save(build); //保存完成,跑到宿舍楼管理列表页,查询所有宿舍楼 List<DormBuild> builds = dormBuildService.find(); //查询所有的宿舍楼,在select中遍历 List<DormBuild> buildSelects = dormBuildService.find(); request.setAttribute("buildSelects", buildSelects); request.setAttribute("builds", builds); request.setAttribute("mainRight", "/WEB-INF/jsp/dormBuildList.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); } } }else if(action != null & action.equals("preUpdate")) { //根据宿舍楼id,查询宿舍楼 DormBuild build = dormBuildService.findById(Integer.parseInt(id)); //保存宿舍楼信息,到前端页面展示 request.setAttribute("build", build); //跳转到宿舍楼修改页面 request.setAttribute("mainRight", "/WEB-INF/jsp/dormBuildAddOrUpdate.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else if(action != null & action.equals("deleteOrAcive")) { //删除或激活 String disabled = request.getParameter("disabled"); DormBuild dormBuild = dormBuildService.findById(Integer.parseInt(id)); System.out.println("更新前dormBuild:"+dormBuild); dormBuild.setDisabled(Integer.parseInt(disabled)); System.out.println("更新后dormBuild:"+dormBuild);
//如当前用户 为宿舍管理员,他只能添加学生到其管理的宿舍楼 //跳转到学生的添加页面 request.setAttribute("mainRight", "/WEB-INF/jsp/studentAddOrUpdate.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else if(action != null & action.equals("save")) { //保存学生 String stuCode = request.getParameter("stuCode"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String tel = request.getParameter("tel"); String passWord = request.getParameter("passWord"); String dormBuildId= request.getParameter("dormBuildId"); String dormCode = request.getParameter("dormCode"); System.out.println("stuCode:"+stuCode+" name:"+name+" sex:"+sex+ " tel:"+tel+" passWord:"+passWord+" dormBuildId:"+dormBuildId+ " dormCode:"+dormCode); //未存在,则保存 User student = userService.findByStuCode(stuCode); if(id != null && !id.equals("")) { //更新之前,查询数据库是否已经存在当前学号的学生,如已存在,则跳转到添加页面 if(student != null && !student.getId().equals(Integer.parseInt(id))) { System.out.println("根据学生学号查询student:"+student); //当前学号的学生已存在,请重新修改,跳转到该学生的修改页面 request.getRequestDispatcher("/student.action?action=preUpdate&id="+id).forward(request, response);; }else { //更新 User studentUpdate = userService.findById(Integer.parseInt(id)); studentUpdate.setStuCode(stuCode); studentUpdate.setSex(sex); studentUpdate.setTel(tel); studentUpdate.setName(name); studentUpdate.setPassWord(passWord); studentUpdate.setDormBuildId(Integer.parseInt(dormBuildId)); studentUpdate.setDormCode(dormCode); userService.updateStudent(studentUpdate); response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=list"); } }else { //保存 if(student != null) { //在保存之前,查询数据库是否已经存在当前学号的学生,如已存在,则跳转到添加页面 response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=preAdd"); }else { User user2 = new User(); user2.setStuCode(stuCode); user2.setSex(sex); user2.setTel(tel);
} } /** * Servlet implementation class RecordServlet */ @WebServlet("/record.action") public class RecordServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public RecordServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response) */ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("====record.action========"); //在tomcat8.0中,如果是post请求,传递过来的中文可能会出现乱码问题 request.setCharacterEncoding("utf-8");
UserService userService = new UserServiceImpl(); RecordService recordService = new RecordServiceImpl(); DormBuildService buildService = new DormBuildServiceImpl(); if(action != null & action.equals("list")) { //查询缺勤记录并展示 Integer roleId = userCurr.getRoleId(); List<DormBuild> builds = new ArrayList<DormBuild>(); if(roleId.equals(0)) { //如当前用户是超级管理员,他能将学生添加到所有的宿舍楼,查询所有宿舍楼 builds = buildService.findAll(); }else if(roleId.equals(1)) { //如当前用户 为宿舍管理员,他只能添加学生到其管理的宿舍楼 builds = buildService.findByUserId(userCurr.getId()); } System.out.println("builds:"+builds); request.setAttribute("builds", builds); String startDate = request.getParameter("startDate"); String endDate = request.getParameter("endDate"); String dormBuildId = request.getParameter("dormBuildId"); String searchType = request.getParameter("searchType"); String keyword = request.getParameter("keyword"); String pageIndex = request.getParameter("pageIndex"); System.out.println("startDate:"+startDate+" endDate:"+endDate+" dormBuildId:"+dormBuildId+ " searchType:"+searchType+" keyword:"+keyword+" pageIndex:"+pageIndex); //默认查询第一页,需两个参数,当前页码pageIndex,每页展示的条数 PageModel pageModel = new PageModel(); if(pageIndex != null && !pageIndex.equals("")) { pageModel.setPageIndex(Integer.parseInt(pageIndex)); } //分页查询考勤记录 List<Record> records = recordService.findRecords(startDate,endDate,dormBuildId, searchType,keyword,userCurr,pageModel); System.out.println("records:"+records); //获取查询结果总数 Integer totalNum = recordService.getToTalNum(startDate,endDate,dormBuildId, searchType,keyword,userCurr); System.out.println("totalNum:"+totalNum); request.setAttribute("totalNum", totalNum); request.setAttribute("startDate", startDate); request.setAttribute("endDate", endDate); request.setAttribute("dormBuildId", dormBuildId); request.setAttribute("searchType", searchType); request.setAttribute("keyword", keyword); request.setAttribute("pageIndex", pageModel.getPageIndex()); request.setAttribute("records", records);
DormBuild dormBuild = dormBuildService.findByName(name); System.out.println("dormBuild:"+dormBuild); if(id != null && !id.equals("")) { //更新 if(dormBuild != null && !dormBuild.getId().equals(Integer.parseInt(id))) { //表示跳转到宿舍楼添加页面 request.setAttribute("error", "当前宿舍楼名已存在,请重新输入!"); //根据宿舍楼id,查询宿舍楼 DormBuild build = dormBuildService.findById(Integer.parseInt(id)); //保存宿舍楼信息,到前端页面展示 request.setAttribute("build", build); request.setAttribute("mainRight", "/WEB-INF/jsp/dormBuildAddOrUpdate.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else { dormBuild = dormBuildService.findById(Integer.parseInt(id)); dormBuild.setName(name); dormBuild.setRemark(remark); //执行更新 dormBuildService.update(dormBuild); //更新完成,跑到宿舍楼管理列表页,查询所有宿舍楼 List<DormBuild> builds = dormBuildService.find(); //查询所有的宿舍楼,在select中遍历 List<DormBuild> buildSelects = dormBuildService.find(); request.setAttribute("buildSelects", buildSelects); request.setAttribute("builds", builds); request.setAttribute("mainRight", "/WEB-INF/jsp/dormBuildList.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); } }else { //保存 if(dormBuild != null) { //当前用户输入的宿舍楼名已存在 //表示跳转到宿舍楼添加页面 request.setAttribute("error", "当前宿舍楼名已存在,请重新输入!"); request.setAttribute("mainRight", "/WEB-INF/jsp/dormBuildAddOrUpdate.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
String oldPassWord = request.getParameter("oldPassWord"); //当前登录的用户原始密码 String passWord = userCur.getPassWord(); if(!passWord.equals(oldPassWord)) { response.setCharacterEncoding("utf-8"); PrintWriter writer = response.getWriter(); writer.print("您输入的原始密码错误,请重新输入!"); } }else if(action != null && action.equals("change")) { //修改密码 String newPassword = request.getParameter("newPassword"); userCur.setPassWord(newPassword); userService.updatePassWord(userCur); System.out.println("修改密码userCur:"+userCur); request.getRequestDispatcher("/index.jsp").forward(request, response); } } } /** * Servlet implementation class LoginOutServlet */ @WebServlet("/loginOut.action") public class LoginOutServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginOutServlet() { super(); // TODO Auto-generated constructor stub }
request.setAttribute("searchType", searchType); request.setAttribute("keyword", keyword); request.setAttribute("users", users); request.setAttribute("mainRight", "dormManagerList.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else if(action != null & action.equals("preAdd")) { //跳转到宿舍管理员添加页面 request.setAttribute("mainRight", "dormManagerAddOrUpdate.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else if(action != null & action.equals("save")) { System.out.println("======保存宿舍管理员========="); String name = request.getParameter("name"); String passWord = request.getParameter("passWord"); String sex = request.getParameter("sex"); String tel = request.getParameter("tel"); //获取复选框中用户选中的宿舍楼 String[] dormBuildIds = request.getParameterValues("dormBuildId"); System.out.println("name:"+name+" pass:"+passWord+" sex:"+sex+" tel:"+tel+" dormBuildIds:"+Arrays.toString(dormBuildIds)); if(id == null || id.equals("")) { //保存 User user = new User(name, passWord, sex, tel, null, 1); user.setDisabled(0); //当前登录的用户 User user2 = (User) request.getSession().getAttribute("session_user"); user.setCreateUserId(user2.getId()); userService.saveManager(user,dormBuildIds); System.out.println("getServletContext().getContextPath():"+getServletContext().getContextPath()); }else { //修改 //通过宿舍管理员ID获取宿舍管理员 User user = userService.findById(Integer.parseInt(id)); user.setName(name); user.setPassWord(passWord); user.setSex(sex); user.setTel(tel); userService.updateManager(user); //修改还需修改宿舍管理员与宿舍楼的中间表 //① 删除当前宿舍管理员管理的所有宿舍楼 buildService.deleteByUserId(user.getId()); //② 新增当前宿舍管理员管理的所有宿舍楼 buildService.saveManagerAndBuild(user.getId(),dormBuildIds); }
User user = userService.findById(Integer.parseInt(id)); //根据宿舍管理员ID获取宿舍管理员管理的楼栋 List<DormBuild> userBuilds = buildService.findByUserId(user.getId()); user.setDormBuilds(userBuilds); System.out.println("user:"+user); List<Integer> userBuildids = new ArrayList<>(); //遍历当前宿舍管理员管理的所有宿舍,获取当时当前宿舍管理员管理的所有宿舍ID for (DormBuild userBuild : userBuilds) { userBuildids.add(userBuild.getId()); } request.setAttribute("userBuildids", userBuildids); request.setAttribute("user", user); request.setAttribute("mainRight", "dormManagerAddOrUpdate.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else if(action != null & action.equals("deleteOrAcive")) { //删除或激活 String disabled =request.getParameter("disabled"); //通过宿舍管理员ID获取宿舍管理员 User user = userService.findById(Integer.parseInt(id)); user.setDisabled(Integer.parseInt(disabled)); userService.updateManager(user); //跳转到宿舍管理员列表页,查看所有的宿舍管理员 //重定向,请求链断开,不能在下一个servlet或jsp中获取保存在request中的参数 response.sendRedirect(getServletContext().getContextPath()+"/dormManager.action?action=list"); } } } /**
/** * Servlet implementation class RecordServlet */ @WebServlet("/record.action") public class RecordServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public RecordServlet() { super(); // TODO Auto-generated constructor stub }
/** * 该过滤器拦截所有以.action的请求 */ @WebFilter("*.action") public class LoginFilter implements Filter { /** * Default constructor. */ public LoginFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("------------登录拦截器------------"); HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpSession session = httpServletRequest.getSession(); //①获取登录后保存在session中的用户信息 User user = (User) session.getAttribute("session_user"); System.out.println("session user:"+user);
public class LoginOutServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginOutServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response) */ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("========退出=========="); //清除保存在session中的用户信息 request.getSession().removeAttribute("session_user"); //清除保存在cookie中的用户信息 //根据名字查找保存当前项目的学号和密码 Cookie cookie = CookieUtil.getCookieByName(request,"cookie_name_pass"); if(cookie != null) { //设计cookie有效时间0 ,马上失效 cookie.setMaxAge(0); //设置cookie作用范围 cookie.setPath(request.getContextPath()); System.out.println("request.getContextPath():"+request.getContextPath()); //将cookie响应出去 response.addCookie(cookie); } response.sendRedirect("index.jsp"); } } public class CookieUtil { public static void addCookie(String cookieName, int time, HttpServletRequest request,HttpServletResponse response, String stuCode, String password) { //根据保存学号和密码的cookie的名字获取cookie Cookie cookie = getCookieByName(request,cookieName); //如有该项目保存学号和密码的cookie,则更改cookie的值,如未有则新建cookie if(cookie != null) { cookie.setValue(stuCode+"#"+password);
response.sendRedirect(getServletContext().getContextPath()+"/record.action?action=list"); }else { request.setAttribute("record", record); request.setAttribute("mainRight", "/WEB-INF/jsp/recordAddOrUpdate.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); } }else if(action != null && action.equals("deleteOrAcive")) { System.out.println("======删除或者激活========="); //删除或者激活 String disabled = request.getParameter("disabled"); Record record = recordService.findById(Integer.parseInt(id)); record.setDisabled(Integer.parseInt(disabled)); //查看用户是否有修改考勤记录的权限 User user = userService.findStuCodeAndManager(record.getUser().getStuCode(),userCurr); System.out.println("修改权限user :"+user); if(user == null) { //无删除激活权限 response.sendRedirect(getServletContext().getContextPath()+"/record.action?action=list"); }else { //有删除激活权限 recordService.update(record); //跳转到缺勤记录列表页 response.sendRedirect(getServletContext().getContextPath()+"/record.action?action=list"); } } } } /** * Servlet implementation class DormBuildServlet */
//将cookie响应出去 response.addCookie(cookie); } response.sendRedirect("index.jsp"); } } public class CookieUtil { public static void addCookie(String cookieName, int time, HttpServletRequest request,HttpServletResponse response, String stuCode, String password) { //根据保存学号和密码的cookie的名字获取cookie Cookie cookie = getCookieByName(request,cookieName); //如有该项目保存学号和密码的cookie,则更改cookie的值,如未有则新建cookie if(cookie != null) { cookie.setValue(stuCode+"#"+password); }else { cookie = new Cookie(cookieName, stuCode+"#"+password); } //设置cookie的有效时间 cookie.setMaxAge(time); //设置cookie的作用范围 只在当前项目范围中有效 cookie.setPath(request.getContextPath()); System.out.println("request.getContextPath():"+request.getContextPath()); response.addCookie(cookie); } public static Cookie getCookieByName(HttpServletRequest request, String cookieName) { //从request中获取当前项目的作用cookie Cookie[] cookies = request.getCookies(); //遍历所有cookie if(cookies != null && cookies.length >0) { for (Cookie cookie : cookies) { //根据cookie的名字获取cookie,并返回 if(cookie.getName().equals(cookieName)) { return cookie;
/** * Servlet implementation class RecordServlet */ @WebServlet("/record.action") public class RecordServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public RecordServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response) */ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("====record.action========"); //在tomcat8.0中,如果是post请求,传递过来的中文可能会出现乱码问题 request.setCharacterEncoding("utf-8"); String action = request.getParameter("action"); String id = request.getParameter("id");
request.setAttribute("pageIndex", pageModel.getPageIndex()); request.setAttribute("records", records); request.setAttribute("mainRight", "/WEB-INF/jsp/recordList.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else if(action != null && action.equals("preAdd")) { //跳转到添加页面 request.setAttribute("mainRight", "/WEB-INF/jsp/recordAddOrUpdate.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); }else if(action != null && action.equals("save")) { //保存 String stuCode = request.getParameter("stuCode"); String date = request.getParameter("date"); String remark = request.getParameter("remark"); System.out.println("remark:"+remark+" date:"+date+" stuCode:"+stuCode); //做保存前,判断用户是否有添加该学号学生缺勤记录的权限 User user = userService.findStuCodeAndManager(stuCode,userCurr); System.out.println("查询添加缺勤权限user:"+user); if(id != null && !id.equals("")) { //更新 if(user != null) { //有修改权限 Record record = recordService.findById(Integer.parseInt(id)); try { record.setDate(new SimpleDateFormat("yyyy-MM-dd").parse(date)); record.setRemark(remark); record.setStudentId(user.getId()); recordService.update(record); } catch (ParseException e) { e.printStackTrace(); } response.sendRedirect(getServletContext().getContextPath()+"/record.action?action=list"); }else { //无修改权限 response.sendRedirect(getServletContext().getContextPath()+"/record.action?action=list"); }
到此这篇关于基于javaweb+mysql的jsp+servlet宿舍管理系统(超级管理员、宿舍管理员、学生)的文章就介绍到这了,更多相关jsp+servlet宿舍管理系统内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!