基于javaweb+mysql的jsp+servlet校园宿舍管理系统(java+jsp+javaweb+mysql+ajax)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的JSP+Servlet校园宿舍管理系统(java+jsp+javaweb+mysql+ajax)
一、项目简述
功能:宿舍管理员,最高管理员,学生三个身份,包括学 生管理,宿舍管理员管理,考勤管理,宿舍楼管理,缺勤 记录管理,个人信息修改等等功能。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。
项目技术: JSP + Entity+ Servlet + html+ css + JavaScript + JQuery + Ajax 等等。
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);
//执行更新
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);
request.setCharacterEncoding("utf-8");
String action = request.getParameter("action");
//获取学生id
String id = request.getParameter("id");
String disabled = request.getParameter("disabled");
//获取当前登录的用户
User user = (User) request.getSession().getAttribute("session_user");
Integer roleId = user.getRoleId();
DormBuildService buildService = new DormBuildServiceImpl();
UserService userService = new UserServiceImpl();
List<DormBuild> builds = new ArrayList<DormBuild>();
if(roleId.equals(0)) {
//如当前用户是超级管理员,他能将学生添加到所有的宿舍楼,查询所有宿舍楼
builds = buildService.findAll();
}else if(roleId.equals(1)) {
//如当前用户 为宿舍管理员,他只能添加学生到其管理的宿舍楼
builds = buildService.findByUserId(user.getId());
}
System.out.println("builds:"+builds);
request.setAttribute("builds", builds);
if(action != null & action.equals("list")) {
//查询学生在右侧展示
String dormBuildId = request.getParameter("dormBuildId");
String searchType = request.getParameter("searchType");
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);
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentServlet() {
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("=========student.action=============");
//解决传递过来的中文乱码问题
request.setCharacterEncoding("utf-8");
String action = request.getParameter("action");
//获取学生id
String id = request.getParameter("id");
String disabled = request.getParameter("disabled");
//获取当前登录的用户
User user = (User) request.getSession().getAttribute("session_user");
Integer roleId = user.getRoleId();
DormBuildService buildService = new DormBuildServiceImpl();
UserService userService = new UserServiceImpl();
List<DormBuild> builds = new ArrayList<DormBuild>();
if(roleId.equals(0)) {
//如当前用户是超级管理员,他能将学生添加到所有的宿舍楼,查询所有宿舍楼
builds = buildService.findAll();
}else if(roleId.equals(1)) {
//如当前用户 为宿舍管理员,他只能添加学生到其管理的宿舍楼
builds = buildService.findByUserId(user.getId());
}
System.out.println("builds:"+builds);
request.setAttribute("builds", builds);
DormBuildService buildService = new DormBuildServiceImpl();
UserService userService = new UserServiceImpl();
List<DormBuild> builds = new ArrayList<DormBuild>();
if(roleId.equals(0)) {
//如当前用户是超级管理员,他能将学生添加到所有的宿舍楼,查询所有宿舍楼
builds = buildService.findAll();
}else if(roleId.equals(1)) {
//如当前用户 为宿舍管理员,他只能添加学生到其管理的宿舍楼
builds = buildService.findByUserId(user.getId());
}
System.out.println("builds:"+builds);
request.setAttribute("builds", builds);
if(action != null & action.equals("list")) {
//查询学生在右侧展示
String dormBuildId = request.getParameter("dormBuildId");
String searchType = request.getParameter("searchType");
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("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);
//未存在,则保存
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");
}
/**
* @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;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
request.setAttribute("mainRight", "/WEB-INF/jsp/studentAddOrUpdate.jsp");
request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
}else {
//表示无修改权限,跳转到学生管理的列表页
response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=list");
}
}else if(action != null & action.equals("deleteOrActive")) {
//删除或激活
User studentUpdate = userService.findById(Integer.parseInt(id));
studentUpdate.setDisabled(Integer.parseInt(disabled));
//判断当前登录的用户是否有修改该学生的权限,如没有跳转到学生管理的列表页;如有则跳转到修改页面
User user2 = userService.findByUserIdAndManager(studentUpdate.getId(),user);
System.out.println("查询删除激活权限user2:"+user2);
if(user2 != null) {
//表示有修改权限
userService.updateStudent(studentUpdate);
response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=list");
}else {
//表示无修改权限,跳转到学生管理的列表页
response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=list");
}
}
}
}
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");
}
}else {
//保存
if(user != null) {
//说明当前登录的用户有添加该学号学生缺勤记录的权限
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = null;
try {
date2 = dateFormat.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Record record = new Record();
record.setStudentId(user.getId());
record.setDate(date2);
record.setDisabled(0);
record.setRemark(remark);
//保存数据到数据库
recordService.save(record);
response.sendRedirect(getServletContext().getContextPath()+"/record.action?action=list");
}else {
//没有添加的权限,跳转到添加页面
request.setAttribute("error", "您没有添加该学号学生缺勤记录的权限!");
request.setAttribute("mainRight", "/WEB-INF/jsp/recordAddOrUpdate.jsp");
request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
}
}
}
}
}
/**
* 该过滤器拦截所有以.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 {
if( (requestUrI.startsWith(path+"/dormBuild.action") || requestUrI.startsWith(path+"/dormManager.action"))
&& roleId.equals(0)) {
//当用户发送的是宿舍楼管理模块或者宿舍管理员管理模块的请求时,只有在当前用户角色为超级管理员时才放行
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
}else if(action != null & action.equals("deleteOrActive")) {
//删除或激活
User studentUpdate = userService.findById(Integer.parseInt(id));
studentUpdate.setDisabled(Integer.parseInt(disabled));
//判断当前登录的用户是否有修改该学生的权限,如没有跳转到学生管理的列表页;如有则跳转到修改页面
User user2 = userService.findByUserIdAndManager(studentUpdate.getId(),user);
System.out.println("查询删除激活权限user2:"+user2);
if(user2 != null) {
//表示有修改权限
userService.updateStudent(studentUpdate);
response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=list");
}else {
//表示无修改权限,跳转到学生管理的列表页
response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=list");
}
}
}
}
dormBuild.setDisabled(Integer.parseInt(disabled));
System.out.println("更新后dormBuild:"+dormBuild);
//执行更新
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);
}
}
}
/**
* Servlet implementation class DormManagerServlet
*/
@WebServlet("/dormManager.action")
public class DormManagerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
//请求链未断开的跳转,可以在下一个servlet或jsp中,获取保存在request中的数据
request.getRequestDispatcher("index.jsp").forward(request, response);
} else {
//用户输入学号和密码正确,登录成功,跳转到主页面
//保存在session中的数据,默认是30分钟内有效。保存在session中的数据,在整个项目中都可以获取得到
request.getSession().setAttribute("session_user", user);
if (remember != null && remember.equals("remember-me")) {
//记住密码一周 时间单位是秒
CookieUtil.addCookie("cookie_name_pass", 7 * 24 * 60 * 60, request, response, URLEncoder.encode(stuCode, "utf-8"), URLEncoder.encode(password, "utf-8"));
}
System.out.println("========跳转到主页面=====");
//WEB-INF下面的内容是受保护的,不能在通过地址栏直接访问,也不能通过response.sendRedirect重定向的形势访问
request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
System.out.println("getServletContext().getContextPath():" + getServletContext().getContextPath());
}
}
}
/**
* Servlet implementation class PassWordServlet
*/
@WebServlet("/password.action")
public class PassWordServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PassWordServlet() {
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("---------------------password.action---------");
User user2 = userService.findByUserIdAndManager(studentUpdate.getId(),user);
System.out.println("查询删除激活权限user2:"+user2);
if(user2 != null) {
//表示有修改权限
userService.updateStudent(studentUpdate);
response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=list");
}else {
//表示无修改权限,跳转到学生管理的列表页
response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=list");
}
}
}
}
/**
* Servlet implementation class RecordServlet
*/
@WebServlet("/record.action")
public class RecordServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
try {
date2 = dateFormat.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Record record = new Record();
record.setStudentId(user.getId());
record.setDate(date2);
record.setDisabled(0);
record.setRemark(remark);
//保存数据到数据库
recordService.save(record);
response.sendRedirect(getServletContext().getContextPath()+"/record.action?action=list");
}else {
//没有添加的权限,跳转到添加页面
request.setAttribute("error", "您没有添加该学号学生缺勤记录的权限!");
request.setAttribute("mainRight", "/WEB-INF/jsp/recordAddOrUpdate.jsp");
request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
}
}
}else if(action != null && action.equals("preUpdate")) {
//跳转到修改页面
Record record = recordService.findById(Integer.parseInt(id));
System.out.println("record:"+record);
//查看用户是否有修改考勤记录的权限
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 {
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));
//宿舍管理员查询
String searchType = request.getParameter("searchType");
String keyword = request.getParameter("keyword");
System.out.println("searchType:"+searchType+" keyword:"+keyword);
//查询宿舍管理员
List<User> users = userService.findManager(searchType,keyword);
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);
}else if(action != null & action.equals("deleteOrActive")) {
//删除或激活
User studentUpdate = userService.findById(Integer.parseInt(id));
studentUpdate.setDisabled(Integer.parseInt(disabled));
//判断当前登录的用户是否有修改该学生的权限,如没有跳转到学生管理的列表页;如有则跳转到修改页面
User user2 = userService.findByUserIdAndManager(studentUpdate.getId(),user);
System.out.println("查询删除激活权限user2:"+user2);
if(user2 != null) {
//表示有修改权限
userService.updateStudent(studentUpdate);
response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=list");
}else {
//表示无修改权限,跳转到学生管理的列表页
response.sendRedirect(getServletContext().getContextPath()+"/student.action?action=list");
}
}
}
}
/**