Java毕业设计之多用户宿舍管理系统的实现

 更新时间:2022年02月22日 08:47:40   作者:编程指南针  
这篇文章主要介绍了基于Java实现的多用户宿舍管理系统,本文采用了jsp、servlet、jdbc等技术,文中示例代码讲解详细,需要的可以参考一下

介绍

项目编号:BS-GX-033

开发技术

后台:jsp+servlet+jdbc

前台:jsp+bootstrap

运行环境

数据库:mysql5.7

JDK: 1.8

Maven: 3.9

开发工具

IDEA /Eclipse

本系统基于jsp实现了一个宿舍管理系统,功能完整,界面美观大方,交互简洁方便,比较适合做毕业设计使用。主要分为三个角色用户:管理员,宿舍管理员,学生

功能介绍

系统管理员:用户管理、宿舍楼管理、楼管管理、缺勤管理。

宿舍管理员主要包括:

缺勤管理、学生管理

学生主要包括:缺勤查看

效果图

系统主要实现功能界面展示如下:

管理员登陆

学生管理

宿舍楼管理

缺勤管理

宿舍管理员登陆

缺勤管理

学生登陆系统

核心代码

核心实现代码如下:

package com.servlet;
 
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 java.io.IOException;
 
@WebServlet(name = "BlankServlet")
public class BlankServlet extends HttpServlet {
    @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 {
        HttpSession session = request.getSession();
        Object currentUserType = session.getAttribute("currentUserType");
        if("admin".equals((String)currentUserType)) {
            request.setAttribute("mainPage", "admin/blank.jsp");
            request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
        } else if("dormManager".equals((String)currentUserType)) {
            request.setAttribute("mainPage", "dormManager/blank.jsp");
            request.getRequestDispatcher("mainManager.jsp").forward(request, response);
        } else if("student".equals((String)currentUserType)) {
            request.setAttribute("mainPage", "student/blank.jsp");
            request.getRequestDispatcher("mainStudent.jsp").forward(request, response);
        }
    }
}
package com.servlet;
 
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
 
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 com.dao.DormBuildDao;
import com.model.DormBuild;
import com.model.DormManager;
import com.model.PageBean;
import com.util.DbUtil;
import com.util.StringUtil;
 
@WebServlet(name = "DormBuildServlet")
public class DormBuildServlet extends HttpServlet {
    DbUtil dbUtil = new DbUtil();
    DormBuildDao dormBuildDao = new DormBuildDao();
 
    @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 {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String s_dormBuildName = request.getParameter("s_dormBuildName");
        String page = request.getParameter("page");
        String action = request.getParameter("action");
        DormBuild dormBuild = new DormBuild();
        if("preSave".equals(action)) {
            dormBuildPreSave(request, response);
            return;
        } else if("save".equals(action)){
            dormBuildSave(request, response);
            return;
        } else if("delete".equals(action)){
            dormBuildDelete(request, response);
            return;
        } else if("manager".equals(action)){
            dormBuildManager(request, response);
            return;
        } else if("addManager".equals(action)){
            dormBuildAddManager(request, response);
        } else if("move".equals(action)){
            managerMove(request, response);
        } else if("list".equals(action)) {
            if(StringUtil.isNotEmpty(s_dormBuildName)) {
                dormBuild.setDormBuildName(s_dormBuildName);
            }
            session.removeAttribute("s_dormBuildName");
            request.setAttribute("s_dormBuildName", s_dormBuildName);
        } else if("search".equals(action)){
            if(StringUtil.isNotEmpty(s_dormBuildName)) {
                dormBuild.setDormBuildName(s_dormBuildName);
                session.setAttribute("s_dormBuildName", s_dormBuildName);
            }else {
                session.removeAttribute("s_dormBuildName");
            }
        } else {
            if(StringUtil.isNotEmpty(s_dormBuildName)) {
                dormBuild.setDormBuildName(s_dormBuildName);
                session.setAttribute("s_dormBuildName", s_dormBuildName);
            }
            if(StringUtil.isEmpty(s_dormBuildName)) {
                Object o = session.getAttribute("s_dormBuildName");
                if(o!=null) {
                    dormBuild.setDormBuildName((String)o);
                }
            }
        }
        if(StringUtil.isEmpty(page)) {
            page="1";
        }
        Connection con = null;
        PageBean pageBean = new PageBean(Integer.parseInt(page), 5);
        request.setAttribute("pageSize", pageBean.getPageSize());
        request.setAttribute("page", pageBean.getPage());
        try {
            con=dbUtil.getCon();
            List<DormBuild> dormBuildList = dormBuildDao.dormBuildList(con, pageBean, dormBuild);
            int total=dormBuildDao.dormBuildCount(con, dormBuild);
            String pageCode = this.genPagation(total, Integer.parseInt(page), 5);
            request.setAttribute("pageCode", pageCode);
            request.setAttribute("dormBuildList", dormBuildList);
            request.setAttribute("mainPage", "admin/dormBuild.jsp");
            request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void managerMove(HttpServletRequest request,
                             HttpServletResponse response) {
        String dormBuildId = request.getParameter("dormBuildId");
        String dormManagerId = request.getParameter("dormManagerId");
        Connection con = null;
        try {
            con = dbUtil.getCon();
            dormBuildDao.managerUpdateWithId(con, dormManagerId, "0");
            request.getRequestDispatcher("dormBuild?action=manager&dormBuildId="+dormBuildId).forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void dormBuildAddManager(HttpServletRequest request,
                                     HttpServletResponse response) {
        String dormBuildId = request.getParameter("dormBuildId");
        String dormManagerId = request.getParameter("dormManagerId");
        Connection con = null;
        try {
            con = dbUtil.getCon();
            dormBuildDao.managerUpdateWithId(con, dormManagerId, dormBuildId);
            request.getRequestDispatcher("dormBuild?action=manager&dormBuildId="+dormBuildId).forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void dormBuildManager(HttpServletRequest request,
                                  HttpServletResponse response) {
        String dormBuildId = request.getParameter("dormBuildId");
        Connection con = null;
        try {
            con = dbUtil.getCon();
            List<DormManager> managerListWithId = dormBuildDao.dormManWithBuildId(con, dormBuildId);
            List<DormManager> managerListToSelect = dormBuildDao.dormManWithoutBuild(con);
            request.setAttribute("dormBuildId", dormBuildId);
            request.setAttribute("managerListWithId", managerListWithId);
            request.setAttribute("managerListToSelect", managerListToSelect);
            request.setAttribute("mainPage", "admin/selectManager.jsp");
            request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void dormBuildDelete(HttpServletRequest request,
                                 HttpServletResponse response) {
        String dormBuildId = request.getParameter("dormBuildId");
        Connection con = null;
        try {
            con = dbUtil.getCon();
            if(dormBuildDao.existManOrDormWithId(con, dormBuildId)) {
                request.setAttribute("error", "宿舍楼下有宿舍或宿管,不能删除该宿舍楼");
            } else {
                dormBuildDao.dormBuildDelete(con, dormBuildId);
            }
            request.getRequestDispatcher("dormBuild?action=list").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void dormBuildSave(HttpServletRequest request,
                               HttpServletResponse response)throws ServletException, IOException {
        String dormBuildId = request.getParameter("dormBuildId");
        String dormBuildName = request.getParameter("dormBuildName");
        String detail = request.getParameter("detail");
        DormBuild dormBuild = new DormBuild(dormBuildName, detail);
        if(StringUtil.isNotEmpty(dormBuildId)) {
            dormBuild.setDormBuildId(Integer.parseInt(dormBuildId));
        }
        Connection con = null;
        try {
            con = dbUtil.getCon();
            int saveNum = 0;
            if(StringUtil.isNotEmpty(dormBuildId)) {
                saveNum = dormBuildDao.dormBuildUpdate(con, dormBuild);
            } else {
                saveNum = dormBuildDao.dormBuildAdd(con, dormBuild);
            }
            if(saveNum > 0) {
                request.getRequestDispatcher("dormBuild?action=list").forward(request, response);
            } else {
                request.setAttribute("dormBuild", dormBuild);
                request.setAttribute("error", "保存失败");
                request.setAttribute("mainPage", "dormBuild/dormBuildSave.jsp");
                request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void dormBuildPreSave(HttpServletRequest request,
                                  HttpServletResponse response)throws ServletException, IOException {
        String dormBuildId = request.getParameter("dormBuildId");
        if(StringUtil.isNotEmpty(dormBuildId)) {
            Connection con = null;
            try {
                con = dbUtil.getCon();
                DormBuild dormBuild = dormBuildDao.dormBuildShow(con, dormBuildId);
                request.setAttribute("dormBuild", dormBuild);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    dbUtil.closeCon(con);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        request.setAttribute("mainPage", "admin/dormBuildSave.jsp");
        request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
    }
 
    private String genPagation(int totalNum, int currentPage, int pageSize){
        int totalPage = totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
        StringBuffer pageCode = new StringBuffer();
        pageCode.append("<li><a href='dormBuild?page=1'>首页</a></li>");
        if(currentPage==1) {
            pageCode.append("<li class='disabled'><a href='#'>上一页</a></li>");
        }else {
            pageCode.append("<li><a href='dormBuild?page="+(currentPage-1)+"'>上一页</a></li>");
        }
        for(int i=currentPage-2;i<=currentPage+2;i++) {
            if(i<1||i>totalPage) {
                continue;
            }
            if(i==currentPage) {
                pageCode.append("<li class='active'><a href='#'>"+i+"</a></li>");
            } else {
                pageCode.append("<li><a href='dormBuild?page="+i+"'>"+i+"</a></li>");
            }
        }
        if(currentPage==totalPage) {
            pageCode.append("<li class='disabled'><a href='#'>下一页</a></li>");
        } else {
            pageCode.append("<li><a href='dormBuild?page="+(currentPage+1)+"'>下一页</a></li>");
        }
        pageCode.append("<li><a href='dormBuild?page="+totalPage+"'>尾页</a></li>");
        return pageCode.toString();
    }
}
package com.servlet;
 
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
 
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 com.dao.DormManagerDao;
import com.model.DormManager;
import com.model.PageBean;
import com.util.DbUtil;
import com.util.StringUtil;
 
@WebServlet(name = "DormManagerServlet")
public class DormManagerServlet extends HttpServlet {
    DbUtil dbUtil = new DbUtil();
    DormManagerDao dormManagerDao = new DormManagerDao();
 
    @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 {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String s_dormManagerText = request.getParameter("s_dormManagerText");
        String searchType = request.getParameter("searchType");
        String page = request.getParameter("page");
        String action = request.getParameter("action");
        DormManager dormManager = new DormManager();
        if("preSave".equals(action)) {
            dormManagerPreSave(request, response);
            return;
        } else if("save".equals(action)){
            dormManagerSave(request, response);
            return;
        } else if("delete".equals(action)){
            dormManagerDelete(request, response);
            return;
        } else
        if("list".equals(action)) {
            if(StringUtil.isNotEmpty(s_dormManagerText)) {
                if("name".equals(searchType)) {
                    dormManager.setName(s_dormManagerText);
                } else if("userName".equals(searchType)) {
                    dormManager.setUserName(s_dormManagerText);
                }
            }
            session.removeAttribute("s_dormManagerText");
            session.removeAttribute("searchType");
            request.setAttribute("s_dormManagerText", s_dormManagerText);
            request.setAttribute("searchType", searchType);
        } else if("search".equals(action)){
            if (StringUtil.isNotEmpty(s_dormManagerText)) {
                if ("name".equals(searchType)) {
                    dormManager.setName(s_dormManagerText);
                } else if ("userName".equals(searchType)) {
                    dormManager.setUserName(s_dormManagerText);
                }
                session.setAttribute("searchType", searchType);
                session.setAttribute("s_dormManagerText", s_dormManagerText);
            } else {
                session.removeAttribute("s_dormManagerText");
                session.removeAttribute("searchType");
            }
        } else {
            if(StringUtil.isNotEmpty(s_dormManagerText)) {
                if("name".equals(searchType)) {
                    dormManager.setName(s_dormManagerText);
                } else if("userName".equals(searchType)) {
                    dormManager.setUserName(s_dormManagerText);
                }
                session.setAttribute("searchType", searchType);
                session.setAttribute("s_dormManagerText", s_dormManagerText);
            }
            if(StringUtil.isEmpty(s_dormManagerText)) {
                Object o1 = session.getAttribute("s_dormManagerText");
                Object o2 = session.getAttribute("searchType");
                if(o1!=null) {
                    if("name".equals((String)o2)) {
                        dormManager.setName((String)o1);
                    } else if("userName".equals((String)o2)) {
                        dormManager.setUserName((String)o1);
                    }
                }
            }
        }
        if(StringUtil.isEmpty(page)) {
            page="1";
        }
        Connection con = null;
        PageBean pageBean = new PageBean(Integer.parseInt(page), 5);
        request.setAttribute("pageSize", pageBean.getPageSize());
        request.setAttribute("page", pageBean.getPage());
        try {
            con=dbUtil.getCon();
            List<DormManager> dormManagerList = dormManagerDao.dormManagerList(con, pageBean, dormManager);
            int total=dormManagerDao.dormManagerCount(con, dormManager);
            String pageCode = this.genPagation(total, Integer.parseInt(page),5);
            request.setAttribute("pageCode", pageCode);
            request.setAttribute("dormManagerList", dormManagerList);
            request.setAttribute("mainPage", "admin/dormManager.jsp");
            request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void dormManagerDelete(HttpServletRequest request,
                                   HttpServletResponse response) {
        String dormManagerId = request.getParameter("dormManagerId");
        Connection con = null;
        try {
            con = dbUtil.getCon();
            dormManagerDao.dormManagerDelete(con, dormManagerId);
            request.getRequestDispatcher("dormManager?action=list").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void dormManagerSave(HttpServletRequest request,
                                 HttpServletResponse response)throws ServletException, IOException {
        String dormManagerId = request.getParameter("dormManagerId");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String tel = request.getParameter("tel");
        DormManager dormManager = new DormManager(userName, password, name, sex, tel);
        if(StringUtil.isNotEmpty(dormManagerId)) {
            dormManager.setDormManagerId(Integer.parseInt(dormManagerId));
        }
        Connection con = null;
        try {
            con = dbUtil.getCon();
            int saveNum = 0;
            if(StringUtil.isNotEmpty(dormManagerId)) {
                saveNum = dormManagerDao.dormManagerUpdate(con, dormManager);
            } else if(dormManagerDao.haveManagerByUser(con, dormManager.getUserName())){
                request.setAttribute("dormManager", dormManager);
                request.setAttribute("error", "该用户名已存在");
                request.setAttribute("mainPage", "admin/dormManagerSave.jsp");
                request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
                try {
                    dbUtil.closeCon(con);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return;
            } else {
                saveNum = dormManagerDao.dormManagerAdd(con, dormManager);
            }
            if(saveNum > 0) {
                request.getRequestDispatcher("dormManager?action=list").forward(request, response);
            } else {
                request.setAttribute("dormManager", dormManager);
                request.setAttribute("error", "保存失败");
                request.setAttribute("mainPage", "admin/dormManagerSave.jsp");
                request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void dormManagerPreSave(HttpServletRequest request,
                                    HttpServletResponse response)throws ServletException, IOException {
        String dormManagerId = request.getParameter("dormManagerId");
        if(StringUtil.isNotEmpty(dormManagerId)) {
            Connection con = null;
            try {
                con = dbUtil.getCon();
                DormManager dormManager = dormManagerDao.dormManagerShow(con, dormManagerId);
                request.setAttribute("dormManager", dormManager);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    dbUtil.closeCon(con);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        request.setAttribute("mainPage", "admin/dormManagerSave.jsp");
        request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
    }
 
    private String genPagation(int totalNum, int currentPage, int pageSize){
        int totalPage = totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
        StringBuffer pageCode = new StringBuffer();
        pageCode.append("<li><a href='dormManager?page=1'>首页</a></li>");
        if(currentPage==1) {
            pageCode.append("<li class='disabled'><a href='#'>上一页</a></li>");
        }else {
            pageCode.append("<li><a href='dormManager?page="+(currentPage-1)+"'>上一页</a></li>");
        }
        for(int i=currentPage-2;i<=currentPage+2;i++) {
            if(i<1||i>totalPage) {
                continue;
            }
            if(i==currentPage) {
                pageCode.append("<li class='active'><a href='#'>"+i+"</a></li>");
            } else {
                pageCode.append("<li><a href='dormManager?page="+i+"'>"+i+"</a></li>");
            }
        }
        if(currentPage==totalPage) {
            pageCode.append("<li class='disabled'><a href='#'>下一页</a></li>");
        } else {
            pageCode.append("<li><a href='dormManager?page="+(currentPage+1)+"'>下一页</a></li>");
        }
        pageCode.append("<li><a href='dormManager?page="+totalPage+"'>尾页</a></li>");
        return pageCode.toString();
    }
}

以上就是Java毕业设计之多用户宿舍管理系统的实现的详细内容,更多关于Java宿舍管理系统的资料请关注脚本之家其它相关文章!

相关文章

  • Java语言中&&与& ||与|的区别是什么

    Java语言中&&与& ||与|的区别是什么

    这篇文章主要介绍了Java语言中&&与& ||与|的区别是什么的相关资料,需要的朋友可以参考下
    2017-04-04
  • java 数据结构中栈结构应用的两个实例

    java 数据结构中栈结构应用的两个实例

    这篇文章主要介绍了java 数据结构中栈结构应用的两个实例的相关资料,需要的朋友可以参考下
    2017-06-06
  • Spring中bean对象的装配方式、作用域及生命周期详解

    Spring中bean对象的装配方式、作用域及生命周期详解

    这篇文章主要介绍了Spring中bean对象的装配方式、作用域及生命周期详解,SprignBoot中 @Bean 完美的替换了了上面的这种在xml中配置的方法,使用以下方法就能让spring在需要自动创建Info对象时,自动调用这个方法,需要的朋友可以参考下
    2023-11-11
  • Springboot整合Dozer实现深度复制的方法

    Springboot整合Dozer实现深度复制的方法

    Dozer是一种Java Bean到Java Bean的映射器,递归地将数据从一个对象复制到另一个对象,它是一个强大的,通用的,灵活的,可重用的和可配置的开源映射框架,本文给大家介绍Springboot整合Dozer的相关知识,感兴趣的朋友跟随小编一起看看吧
    2022-03-03
  • 浅析MMAP零拷贝在RocketMQ中的运用

    浅析MMAP零拷贝在RocketMQ中的运用

    零拷贝技术可以减少数据拷贝和共享总线操作的次数,消除传输数据在存储器之间不必要的中间拷贝次数,从而有效地提高数据传输效率,这篇文章主要介绍了MMAP零拷贝在RocketMQ中的运用,需要的朋友可以参考下
    2022-07-07
  • 最好的Java 反编译工具的使用对比分析

    最好的Java 反编译工具的使用对比分析

    恰好最近工作中也需要用到 Java 反编译,所以这篇文章介绍目前常见的的几种 Java 反编译工具的使用,在文章的最后也会通过编译速度、语法支持以及代码可读性三个维度,对它们进行测试,分析几款工具的优缺点,感兴趣的朋友一起看看吧
    2021-05-05
  • 详解java生成json字符串的方法

    详解java生成json字符串的方法

    本篇文章主要介绍了java生成json字符串的方法,包括map对象转换成json对象,list转换成json,json转换成list和map,有兴趣的可以了解一下。
    2017-01-01
  • java.lang.Instrument 代理Agent使用详细介绍

    java.lang.Instrument 代理Agent使用详细介绍

    这篇文章主要介绍了java.lang.Instrument 代理Agent使用详细介绍的相关资料,附有实例代码,帮助大家学习参考,需要的朋友可以参考下
    2016-11-11
  • java实现CSV 字段分割

    java实现CSV 字段分割

    这篇文章主要介绍了java实现CSV 字段分割的相关资料,需要的朋友可以参考下
    2015-07-07
  • java网上图书商城(5)购物车模块2

    java网上图书商城(5)购物车模块2

    这篇文章主要为大家详细介绍了java网上图书商城,购物车模块第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12

最新评论