Bootstrap

用户管理系统

一、项目介绍

本项目是基于JavaWeb工程的用户管理系统,编写的目的在于更好的学习JavaWeb,博客最后附上项目的地址,欢迎大家下载。

二、项目功能介绍

1、登陆页面

使用了bootstrap.css来优化页面,使用session来存储用户的账号和密码

2、注册页面

与登陆页面相似,也使用了bootstrap.css,注册成功替换原先的cookie值

3、用户信息列表页面

从数据库中获取数据,并显示在页面中,并实现了分页加载技术

@WebServlet(urlPatterns = "/pageQueryServlet")
public class PageQueryServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strPageNum = request.getParameter("pageNum");
        String strPageSize = request.getParameter("pageSize");
        UserService userService = new UserService();
        //当前页的页数
        int pageNum = Integer.parseInt(strPageNum);
        //每页显示的个数
        int pageSize = Integer.parseInt(strPageSize);
        //每页的起始条目索引
        int startCount = (pageNum - 1) * pageSize;
        List<User> pageUsers = userService.pageQuery(startCount, pageSize);

        int totalItemCount = userService.queryTotalItemCount(pageSize);
        int totalPageCount;
        //计算总页数
        if (totalItemCount % pageSize == 0) {
            totalPageCount = totalItemCount % pageSize;
        } else {
            totalPageCount = totalItemCount / pageSize + 1;
        }

        if (pageUsers != null) {
            //查询成功
            request.setAttribute("pageUsers", pageUsers);
            request.setAttribute("pageNum", pageNum);
            request.setAttribute("pageSize", pageSize);
            request.setAttribute("totalPageCount", totalPageCount);
            request.setAttribute("totalItemCount", totalItemCount);
            request.getRequestDispatcher("/list.jsp").forward(request, response);
        }
    }
}

4、修改联系人页面

实现了数据的回显功能,但是没有做数据的校验,可以使用jQuery Validate的进行表单校验来优化项目

数据回显代码:

@WebServlet(urlPatterns = "/queryUserByIdServlet")
public class QueryUserByIdServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理修改用户信息之前的查询操作
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        //根据ID获取用户信息
        UserService userService = new UserService();
        User userInfo = userService.queryUserById(id);
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        if (userInfo != null) {
            //查询成功
            request.setAttribute("user", userInfo);
            request.setAttribute("pageNum", pageNum);
            request.setAttribute("pageSize", pageSize);
            request.getRequestDispatcher("/update.jsp").forward(request, response);
        } else {
            request.setAttribute("errorMsg", "查询用户信息失败!");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
        }
    }
}

修改数据代码:

@WebServlet(urlPatterns = "/updateUserByIdServlet")
public class UpdateUserByIdServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获取数据
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String address = request.getParameter("address");
        String qq = request.getParameter("qq");
        String email = request.getParameter("email");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        //处理数据
        User user = new User(Integer.parseInt(id), name, sex, Integer.parseInt(age), address, qq, email);
        UserService userService = new UserService();
        boolean isUpdate = userService.updateUserById(user);
        if (isUpdate) {
            //修改成功,查询所有用户信息
            response.sendRedirect("/pageQueryServlet?pageNum=" + pageNum + "&pageSize=" + pageSize);
        } else {
            //修改失败
            request.setAttribute("errorMsg", "修改用户信息失败");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
        }
    }
}

5、删除用户功能

@WebServlet(urlPatterns = "/deleteUserByIdServlet")
public class DeleteUserByIdServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        request.setCharacterEncoding("utf-8");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        //获取数据
        String id = request.getParameter("id");
        //处理数据
        UserService userService = new UserService();
        boolean isDelete = userService.deleteUserById(id);
        if (isDelete) {
            //删除成功,需要查询所有用户信息
            response.sendRedirect("/pageQueryServlet?pageNum=" + pageNum + "&pageSize=" + pageSize);
        } else {
            //删除失败,跳到错误页面
            request.setAttribute("errorMsg", "删除用户信息失败");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
        }
    }
}

6、添加用户页面

和修改联系人页面一样,没有做表单校验。

提交代码:

@WebServlet(urlPatterns = "/addUserServlet")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获取数据
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String address = request.getParameter("address");
        String qq = request.getParameter("qq");
        String email = request.getParameter("email");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        //处理数据
        User user = new User(null, name, sex, Integer.parseInt(age), address, qq, email);
        UserService userService = new UserService();
        boolean isAdd = userService.addUser(user);
        if (isAdd) {
            //添加成功,需要查询所有用户信息
            response.sendRedirect("/pageQueryServlet?pageNum=" + pageNum + "&pageSize=" + pageSize);
        } else {
            //添加失败,跳转失败页面
            request.setAttribute("errorMsg", "添加用户信息失败!");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
        }
    }
}

项目地址:https://github.com/demon-gu/user-manager

;