基于javaweb+mysql的jsp+servlet共享单车管理系统(JavaWeb JSP MySQL Servlet SSM SpringBoot Bootstrap Ajax)

基于javaweb+mysql的jsp+servlet共享单车管理系统(JavaWeb JSP MySQL Servlet SSM SpringBoot Bootstrap Ajax)

运行环境

Java≥8、MySQL≥5.7、Tomcat≥8

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

技术框架

JavaBean MVC JSP Servlet MySQL CSS JavaScript Bootstrap Ajax

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

登录、注册、退出、用户模块、公告模块、单车模块、报修模块的增删改查管理

eclipse/MyEclipse运行:

idea运行:

            //调用Service层增加方法(add),增加记录
            userService.add(vo);
            this.redirectList(request, response);
        } else if ("delete".equals(action)) {//删除
            //取出表要删除的用户记录的主键
            long id = Long.parseLong(request.getParameter("id"));
            UserService userService = new UserServiceImpl();
            //调用Service层删除方法(delete),将对应的记录删除
            userService.delete(id);
            this.redirectList(request, response);
        } else if ("edit".equals(action)) {//修改
            //取出页面传进来的各个数据,并设置到User对象的属性里
            User vo = new User();
            vo.setId(Long.valueOf(request.getParameter("id")));
            vo.setUsername(request.getParameter("username"));
            vo.setPassword(request.getParameter("password"));
            vo.setRealName(request.getParameter("realName"));
            vo.setUserSex(request.getParameter("userSex"));
            vo.setUserPhone(request.getParameter("userPhone"));
            vo.setUserText(request.getParameter("userText"));
            vo.setUserType(request.getParameter("userType"));
            UserService userService = new UserServiceImpl();
            //调用Service层更新方法(update),更新记录
            userService.update(vo);
            this.redirectList(request, response);
        } else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
            Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
            UserService userService = new UserServiceImpl();
            User vo = userService.get(id);
            response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(vo));
        } else {//默认去列表页面
            this.redirectList(request, response);
        }
    }

    /**
     * 处理Get请求
     *
     * @param request
     * @param response
        pb.setSearchColumn(searchColumn);
        pb.setKeyword(keyword);
        pb.setList(list);
        request.getSession().setAttribute("pageBean", pb);
        request.getSession().setAttribute("list", pb.getList());

        response.sendRedirect("user_list.jsp");
    }
}
package com.demo.servlet;

/**
 * 公告模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用NoticeService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/NoticeServlet")
public class NoticeServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("add".equals(action)) {//增加
            Notice vo = new Notice();
            //取出页面传进来的各个数据,并设置到Notice对象的属性里
            vo.setNoticeName(request.getParameter("noticeName"));

    /**
     * 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
     *
     * @param request
     * @param response
     */
    private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //查询列和关键字
        String searchColumn = request.getParameter("searchColumn");
        String keyword = request.getParameter("keyword");
        Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
        params.put("searchColumn", searchColumn);//要查询的列
        params.put("keyword", keyword);//查询的关键字
        UserService userService = new UserServiceImpl();
        Map<String, Object> map = userService.list(params);
        request.getSession().setAttribute("list", map.get("list"));

        Integer totalRecord = (Integer) map.get("totalCount");//根据查询条件取出对应的总记录数,用于分页
        String pageNum = request.getParameter("pageNum");//封装分页参数
        com.demo.util.PageBean<Object> pb = new com.demo.util.PageBean(Integer.valueOf(pageNum != null ? pageNum : "1"), totalRecord);
        params.put("startIndex", pb.getStartIndex());
        params.put("pageSize", pb.getPageSize());
        List list = (List) userService.list(params).get("list");//根据分页参数startIndex、pageSize查询出来的最终结果list
        pb.setServlet("UserServlet");
        pb.setSearchColumn(searchColumn);
        pb.setKeyword(keyword);
        pb.setList(list);
        request.getSession().setAttribute("pageBean", pb);
        request.getSession().setAttribute("list", pb.getList());

        response.sendRedirect("user_list.jsp");
    }
}
package com.demo.servlet;

/**
package com.demo.servlet;

/**
 * 报修模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用XiuService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/XiuServlet")
public class XiuServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("add".equals(action)) {//增加
            Xiu vo = new Xiu();
            //取出页面传进来的各个数据,并设置到Xiu对象的属性里
            vo.setXiuName(request.getParameter("xiuName"));
            vo.setXiuReason(request.getParameter("xiuReason"));
    private Color getRandomColor(int minColor, int maxColor) {
        Random random = new Random();
        // 保存minColor最大不会超过255
        if (minColor > 255)
            minColor = 255;
        //  保存minColor最大不会超过255
        if (maxColor > 255)
            maxColor = 255;
        //  获得红色的随机颜色值
        int red = minColor + random.nextInt(maxColor - minColor);
        //  获得绿色的随机颜色值
        int green = minColor + random.nextInt(maxColor - minColor);
        //  获得蓝色的随机颜色值
        int blue = minColor + random.nextInt(maxColor - minColor);
        return new Color(red, green, blue);
    }
}
package com.demo.servlet;

/**
 * 单车模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用CheService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/CheServlet")
public class CheServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");

/**
 * 报修模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用XiuService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/XiuServlet")
public class XiuServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("add".equals(action)) {//增加
            Xiu vo = new Xiu();
            //取出页面传进来的各个数据,并设置到Xiu对象的属性里
            vo.setXiuName(request.getParameter("xiuName"));
            vo.setXiuReason(request.getParameter("xiuReason"));
            vo.setXiuDate(request.getParameter("xiuDate"));
            vo.setXiuHandledate(request.getParameter("xiuHandledate"));
            vo.setXiuRen(request.getParameter("xiuRen"));
            vo.setXiuPhone(request.getParameter("xiuPhone"));
            vo.setXiuStatus(request.getParameter("xiuStatus"));
            vo.setXiuText(request.getParameter("xiuText"));
            XiuService xiuService = new XiuServiceImpl();
            //调用Service层增加方法(add),增加记录
            xiuService.add(vo);
            this.redirectList(request, response);
        } else if ("delete".equals(action)) {//删除
            //取出表要删除的报修记录的主键
            long id = Long.parseLong(request.getParameter("id"));
            XiuService xiuService = new XiuServiceImpl();
            //调用Service层删除方法(delete),将对应的记录删除
            xiuService.delete(id);
            this.redirectList(request, response);
        } else if ("edit".equals(action)) {//修改
            //取出页面传进来的各个数据,并设置到Xiu对象的属性里
            Xiu vo = new Xiu();
            vo.setId(Long.valueOf(request.getParameter("id")));
            vo.setXiuName(request.getParameter("xiuName"));
            vo.setXiuReason(request.getParameter("xiuReason"));
            vo.setXiuDate(request.getParameter("xiuDate"));
            vo.setXiuHandledate(request.getParameter("xiuHandledate"));
            vo.setXiuRen(request.getParameter("xiuRen"));
            vo.setId(Long.valueOf(request.getParameter("id")));
            vo.setXiuName(request.getParameter("xiuName"));
            vo.setXiuReason(request.getParameter("xiuReason"));
            vo.setXiuDate(request.getParameter("xiuDate"));
            vo.setXiuHandledate(request.getParameter("xiuHandledate"));
            vo.setXiuRen(request.getParameter("xiuRen"));
            vo.setXiuPhone(request.getParameter("xiuPhone"));
            vo.setXiuStatus(request.getParameter("xiuStatus"));
            vo.setXiuText(request.getParameter("xiuText"));
            XiuService xiuService = new XiuServiceImpl();
            //调用Service层更新方法(update),更新记录
            xiuService.update(vo);
            this.redirectList(request, response);
        } else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
            Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
            XiuService xiuService = new XiuServiceImpl();
            Xiu vo = xiuService.get(id);
            response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(vo));
        } else {//默认去列表页面
            this.redirectList(request, response);
        }
    }

    /**
     * 处理Get请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
    }

    /**
     * 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
     *
     * @param request
     * @param response
     */
    private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //查询列和关键字
        String searchColumn = request.getParameter("searchColumn");
        String keyword = request.getParameter("keyword");
        Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
        params.put("searchColumn", searchColumn);//要查询的列

/**
 * 用户模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用UserService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("add".equals(action)) {//增加
            User vo = new User();
            //取出页面传进来的各个数据,并设置到User对象的属性里
            vo.setUsername(request.getParameter("username"));
            vo.setPassword(request.getParameter("password"));
            vo.setRealName(request.getParameter("realName"));
            vo.setUserSex(request.getParameter("userSex"));
            vo.setUserPhone(request.getParameter("userPhone"));
            vo.setUserText(request.getParameter("userText"));
            vo.setUserType(request.getParameter("userType"));
            UserService userService = new UserServiceImpl();
            //调用Service层增加方法(add),增加记录
            userService.add(vo);
            this.redirectList(request, response);
        } else if ("delete".equals(action)) {//删除
            //取出表要删除的用户记录的主键
            long id = Long.parseLong(request.getParameter("id"));
            UserService userService = new UserServiceImpl();
            //调用Service层删除方法(delete),将对应的记录删除
            userService.delete(id);
            this.redirectList(request, response);
            List<User> list = (List<User>) userService.list(params).get("list");
            for (User user : list) {
                if (user.getUsername().equals(username) /*&& user.getPassword().equals(password)*/) {//说明该用户名已存在,必须换个用户名才能注册
                    request.getSession().setAttribute("alert_msg", "错误:用户名已存在!");
                    request.getRequestDispatcher("register.jsp").forward(request, response);
                    return;
                }
            }
            User vo = new User();
            vo.setUsername(username);
            vo.setPassword(password);
            //vo.setUserType("普通用户");//需要设置一个默认值
            userService.add(vo);
            request.getSession().setAttribute("alert_msg", "注册成功!用户名:[" + username + "]");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else if ("logout".equalsIgnoreCase(action)) {//登出
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("loginUser");
            if (user != null) {
                session.removeAttribute("loginUser");
            }
            response.sendRedirect("login.jsp");
        } else if ("validationCode".equalsIgnoreCase(action)) {
            String codeChars = "0123456789";// 图形验证码的字符集合,系统将随机从这个字符串中选择一些字符作为验证码
            //  获得验证码集合的长度
            int charsLength = codeChars.length();
            //  下面三条记录是关闭客户端浏览器的缓冲区
            //  这三条语句都可以关闭浏览器的缓冲区,但是由于浏览器的版本不同,对这三条语句的支持也不同
            //  因此,为了保险起见,建议同时使用这三条语句来关闭浏览器的缓冲区
            response.setHeader("ragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            //  设置图形验证码的长和宽(图形的大小)
            int width = 90, height = 20;
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = image.getGraphics();//  获得用于输出文字的Graphics对象
            Random random = new Random();
            g.setColor(getRandomColor(180, 250));// 随机设置要填充的颜色
            g.fillRect(0, 0, width, height);//  填充图形背景
            //  设置初始字体
            g.setFont(new Font("Times New Roman", Font.ITALIC, height));
            g.setColor(getRandomColor(120, 180));// 随机设置字体颜色
            //  用于保存最后随机生成的验证码
            StringBuilder validationCode = new StringBuilder();
            //  验证码的随机字体
            cheService.delete(id);
            this.redirectList(request, response);
        } else if ("edit".equals(action)) {//修改
            //取出页面传进来的各个数据,并设置到Che对象的属性里
            Che vo = new Che();
            vo.setId(Long.valueOf(request.getParameter("id")));
            vo.setCheName(request.getParameter("cheName"));
            vo.setCheArea(request.getParameter("cheArea"));
            vo.setCheDate(request.getParameter("cheDate"));
            vo.setCheRen(request.getParameter("cheRen"));
            vo.setChePhone(request.getParameter("chePhone"));
            vo.setCheType(request.getParameter("cheType"));
            vo.setCheWei(request.getParameter("cheWei"));
            vo.setCheStatus(request.getParameter("cheStatus"));
            vo.setCheText(request.getParameter("cheText"));
            CheService cheService = new CheServiceImpl();
            //调用Service层更新方法(update),更新记录
            cheService.update(vo);
            this.redirectList(request, response);
        } else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
            Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
            CheService cheService = new CheServiceImpl();
            Che vo = cheService.get(id);
            response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(vo));
        } else {//默认去列表页面
            this.redirectList(request, response);
        }
    }

    /**
     * 处理Get请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
    }

    /**
     * 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
     *
     * @param request
     * @param response
     */
    private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //查询列和关键字
        String searchColumn = request.getParameter("searchColumn");
        String keyword = request.getParameter("keyword");
            vo.setUserSex(request.getParameter("userSex"));
            vo.setUserPhone(request.getParameter("userPhone"));
            vo.setUserText(request.getParameter("userText"));
            vo.setUserType(request.getParameter("userType"));
            UserService userService = new UserServiceImpl();
            //调用Service层更新方法(update),更新记录
            userService.update(vo);
            this.redirectList(request, response);
        } else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
            Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
            UserService userService = new UserServiceImpl();
            User vo = userService.get(id);
            response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(vo));
        } else {//默认去列表页面
            this.redirectList(request, response);
        }
    }

    /**
     * 处理Get请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
    }

    /**
     * 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
     *
     * @param request
     * @param response
     */
    private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //查询列和关键字
        String searchColumn = request.getParameter("searchColumn");
        String keyword = request.getParameter("keyword");
        Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
        params.put("searchColumn", searchColumn);//要查询的列
        params.put("keyword", keyword);//查询的关键字
        UserService userService = new UserServiceImpl();
        Map<String, Object> map = userService.list(params);
        request.getSession().setAttribute("list", map.get("list"));

        Integer totalRecord = (Integer) map.get("totalCount");//根据查询条件取出对应的总记录数,用于分页
        String pageNum = request.getParameter("pageNum");//封装分页参数

/**
 * 公告模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用NoticeService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/NoticeServlet")
public class NoticeServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("add".equals(action)) {//增加
            Notice vo = new Notice();
            //取出页面传进来的各个数据,并设置到Notice对象的属性里
            vo.setNoticeName(request.getParameter("noticeName"));
            vo.setNoticeText(request.getParameter("noticeText"));
            vo.setNoticeType(request.getParameter("noticeType"));
            vo.setCreateDate(request.getParameter("createDate"));
            NoticeService noticeService = new NoticeServiceImpl();
            //调用Service层增加方法(add),增加记录
            noticeService.add(vo);
            this.redirectList(request, response);
        } else if ("delete".equals(action)) {//删除
            //取出表要删除的公告记录的主键
            long id = Long.parseLong(request.getParameter("id"));
            NoticeService noticeService = new NoticeServiceImpl();
            //调用Service层删除方法(delete),将对应的记录删除
            noticeService.delete(id);
            this.redirectList(request, response);
        } else if ("edit".equals(action)) {//修改
            //取出页面传进来的各个数据,并设置到Notice对象的属性里
            Notice vo = new Notice();
            vo.setId(Long.valueOf(request.getParameter("id")));

            UserService userService = new UserServiceImpl();
            Map<String, Object> params = new HashMap();
            
            
            List<User> list = (List<User>) userService.list(params).get("list");
            for (User user : list) {
                if (user.getUsername().equals(username) && user.getPassword().equals(password)) {//找到这个管理员了
                    request.getSession().setAttribute("loginUser", user);
					request.getSession().setMaxInactiveInterval(Integer.MAX_VALUE);
                    request.getRequestDispatcher("UserServlet").forward(request, response);
                    return;
                }
            }
            request.getSession().setAttribute("alert_msg", "错误:用户名或密码错误!");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else if ("register".equalsIgnoreCase(action)) {//注册
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            System.out.println("username=" + username);
            System.out.println("password=" + password);
            UserService userService = new UserServiceImpl();
            Map<String, Object> params = new HashMap();
            
            
            params.put("startIndex", 0);
            params.put("pageSize", Long.MAX_VALUE);
            List<User> list = (List<User>) userService.list(params).get("list");
            for (User user : list) {
                if (user.getUsername().equals(username) /*&& user.getPassword().equals(password)*/) {//说明该用户名已存在,必须换个用户名才能注册
                    request.getSession().setAttribute("alert_msg", "错误:用户名已存在!");
                    request.getRequestDispatcher("register.jsp").forward(request, response);
                    return;
                }
            }
            User vo = new User();
            vo.setUsername(username);
            vo.setPassword(password);
            //vo.setUserType("普通用户");//需要设置一个默认值
            userService.add(vo);
            request.getSession().setAttribute("alert_msg", "注册成功!用户名:[" + username + "]");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else if ("logout".equalsIgnoreCase(action)) {//登出
    }

    /**
     * 处理Get请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    // 返回一个随机颜色(Color对象)
    private Color getRandomColor(int minColor, int maxColor) {
        Random random = new Random();
        // 保存minColor最大不会超过255
        if (minColor > 255)
            minColor = 255;
        //  保存minColor最大不会超过255
        if (maxColor > 255)
            maxColor = 255;
        //  获得红色的随机颜色值
        int red = minColor + random.nextInt(maxColor - minColor);
        //  获得绿色的随机颜色值
        int green = minColor + random.nextInt(maxColor - minColor);
        //  获得蓝色的随机颜色值
        int blue = minColor + random.nextInt(maxColor - minColor);
        return new Color(red, green, blue);
    }
}
package com.demo.servlet;

        Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
        params.put("searchColumn", searchColumn);//要查询的列
        params.put("keyword", keyword);//查询的关键字
        XiuService xiuService = new XiuServiceImpl();
        Map<String, Object> map = xiuService.list(params);
        request.getSession().setAttribute("list", map.get("list"));

        Integer totalRecord = (Integer) map.get("totalCount");//根据查询条件取出对应的总记录数,用于分页
        String pageNum = request.getParameter("pageNum");//封装分页参数
        com.demo.util.PageBean<Object> pb = new com.demo.util.PageBean(Integer.valueOf(pageNum != null ? pageNum : "1"), totalRecord);
        params.put("startIndex", pb.getStartIndex());
        params.put("pageSize", pb.getPageSize());
        List list = (List) xiuService.list(params).get("list");//根据分页参数startIndex、pageSize查询出来的最终结果list
        pb.setServlet("XiuServlet");
        pb.setSearchColumn(searchColumn);
        pb.setKeyword(keyword);
        pb.setList(list);
        request.getSession().setAttribute("pageBean", pb);
        request.getSession().setAttribute("list", pb.getList());

        response.sendRedirect("xiu_list.jsp");
    }
}
package com.demo.servlet;

/**
 * 用户模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用UserService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {

    /**
     * 处理Post请求
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("add".equals(action)) {//增加
            Che vo = new Che();
            //取出页面传进来的各个数据,并设置到Che对象的属性里
            vo.setCheName(request.getParameter("cheName"));
            vo.setCheArea(request.getParameter("cheArea"));
            vo.setCheDate(request.getParameter("cheDate"));
            vo.setCheRen(request.getParameter("cheRen"));
            vo.setChePhone(request.getParameter("chePhone"));
            vo.setCheType(request.getParameter("cheType"));
            vo.setCheWei(request.getParameter("cheWei"));
            vo.setCheStatus(request.getParameter("cheStatus"));
            vo.setCheText(request.getParameter("cheText"));
            CheService cheService = new CheServiceImpl();
            //调用Service层增加方法(add),增加记录
            cheService.add(vo);
            this.redirectList(request, response);
        } else if ("delete".equals(action)) {//删除
            //取出表要删除的单车记录的主键
            long id = Long.parseLong(request.getParameter("id"));
            CheService cheService = new CheServiceImpl();
            //调用Service层删除方法(delete),将对应的记录删除
            cheService.delete(id);
            this.redirectList(request, response);
        } else if ("edit".equals(action)) {//修改
            //取出页面传进来的各个数据,并设置到Che对象的属性里
            Che vo = new Che();
            vo.setId(Long.valueOf(request.getParameter("id")));
            vo.setCheName(request.getParameter("cheName"));
            vo.setCheArea(request.getParameter("cheArea"));
            vo.setCheDate(request.getParameter("cheDate"));
            vo.setCheRen(request.getParameter("cheRen"));
            vo.setChePhone(request.getParameter("chePhone"));
            vo.setCheType(request.getParameter("cheType"));
            vo.setCheWei(request.getParameter("cheWei"));
            vo.setCheStatus(request.getParameter("cheStatus"));
            vo.setCheText(request.getParameter("cheText"));
            CheService cheService = new CheServiceImpl();
        doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
    }

    /**
     * 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
     *
     * @param request
     * @param response
     */
    private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //查询列和关键字
        String searchColumn = request.getParameter("searchColumn");
        String keyword = request.getParameter("keyword");
        Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
        params.put("searchColumn", searchColumn);//要查询的列
        params.put("keyword", keyword);//查询的关键字
        XiuService xiuService = new XiuServiceImpl();
        Map<String, Object> map = xiuService.list(params);
        request.getSession().setAttribute("list", map.get("list"));

        Integer totalRecord = (Integer) map.get("totalCount");//根据查询条件取出对应的总记录数,用于分页
        String pageNum = request.getParameter("pageNum");//封装分页参数
        com.demo.util.PageBean<Object> pb = new com.demo.util.PageBean(Integer.valueOf(pageNum != null ? pageNum : "1"), totalRecord);
        params.put("startIndex", pb.getStartIndex());
        params.put("pageSize", pb.getPageSize());
        List list = (List) xiuService.list(params).get("list");//根据分页参数startIndex、pageSize查询出来的最终结果list
        pb.setServlet("XiuServlet");
        pb.setSearchColumn(searchColumn);
        pb.setKeyword(keyword);
        pb.setList(list);
        request.getSession().setAttribute("pageBean", pb);
        request.getSession().setAttribute("list", pb.getList());

        response.sendRedirect("xiu_list.jsp");
    }
}
package com.demo.servlet;

            String username = request.getParameter("username");
            String password = request.getParameter("password");

            String validationCode = request.getParameter("validationCode");
            if (validationCode != null && !validationCode.equals(request.getSession().getAttribute("validationCode"))) {//验证码不通过
                request.getSession().setAttribute("alert_msg", "错误:验证码不正确!");
                request.getRequestDispatcher("login.jsp").forward(request, response);
                return;
            }

            UserService userService = new UserServiceImpl();
            Map<String, Object> params = new HashMap();
            
            
            List<User> list = (List<User>) userService.list(params).get("list");
            for (User user : list) {
                if (user.getUsername().equals(username) && user.getPassword().equals(password)) {//找到这个管理员了
                    request.getSession().setAttribute("loginUser", user);
					request.getSession().setMaxInactiveInterval(Integer.MAX_VALUE);
                    request.getRequestDispatcher("UserServlet").forward(request, response);
                    return;
                }
            }
            request.getSession().setAttribute("alert_msg", "错误:用户名或密码错误!");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else if ("register".equalsIgnoreCase(action)) {//注册
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            System.out.println("username=" + username);
            System.out.println("password=" + password);
            UserService userService = new UserServiceImpl();
            Map<String, Object> params = new HashMap();
            
            
            params.put("startIndex", 0);
            params.put("pageSize", Long.MAX_VALUE);
            List<User> list = (List<User>) userService.list(params).get("list");
            for (User user : list) {
                if (user.getUsername().equals(username) /*&& user.getPassword().equals(password)*/) {//说明该用户名已存在,必须换个用户名才能注册
                    request.getSession().setAttribute("alert_msg", "错误:用户名已存在!");

/**
 * 报修模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用XiuService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/XiuServlet")
public class XiuServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("add".equals(action)) {//增加
            Xiu vo = new Xiu();
            //取出页面传进来的各个数据,并设置到Xiu对象的属性里
            vo.setXiuName(request.getParameter("xiuName"));
            vo.setXiuReason(request.getParameter("xiuReason"));
            vo.setXiuDate(request.getParameter("xiuDate"));
            vo.setXiuHandledate(request.getParameter("xiuHandledate"));
            vo.setXiuRen(request.getParameter("xiuRen"));
            vo.setXiuPhone(request.getParameter("xiuPhone"));
            vo.setXiuStatus(request.getParameter("xiuStatus"));
            vo.setXiuText(request.getParameter("xiuText"));
            XiuService xiuService = new XiuServiceImpl();
            //调用Service层增加方法(add),增加记录
            xiuService.add(vo);
            this.redirectList(request, response);
        } else if ("delete".equals(action)) {//删除
            //取出表要删除的报修记录的主键
            long id = Long.parseLong(request.getParameter("id"));
            XiuService xiuService = new XiuServiceImpl();
            //调用Service层删除方法(delete),将对应的记录删除
            xiuService.delete(id);
            this.redirectList(request, response);
        } else if ("edit".equals(action)) {//修改
            //取出页面传进来的各个数据,并设置到Xiu对象的属性里
            Xiu vo = new Xiu();
            vo.setId(Long.valueOf(request.getParameter("id")));
            vo.setXiuName(request.getParameter("xiuName"));
            vo.setXiuReason(request.getParameter("xiuReason"));
            vo.setXiuDate(request.getParameter("xiuDate"));
        com.demo.util.PageBean<Object> pb = new com.demo.util.PageBean(Integer.valueOf(pageNum != null ? pageNum : "1"), totalRecord);
        params.put("startIndex", pb.getStartIndex());
        params.put("pageSize", pb.getPageSize());
        List list = (List) xiuService.list(params).get("list");//根据分页参数startIndex、pageSize查询出来的最终结果list
        pb.setServlet("XiuServlet");
        pb.setSearchColumn(searchColumn);
        pb.setKeyword(keyword);
        pb.setList(list);
        request.getSession().setAttribute("pageBean", pb);
        request.getSession().setAttribute("list", pb.getList());

        response.sendRedirect("xiu_list.jsp");
    }
}
package com.demo.servlet;

/**
 * 用户模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用UserService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
 */
//@WebServlet("/NoticeServlet")
public class NoticeServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("add".equals(action)) {//增加
            Notice vo = new Notice();
            //取出页面传进来的各个数据,并设置到Notice对象的属性里
            vo.setNoticeName(request.getParameter("noticeName"));
            vo.setNoticeText(request.getParameter("noticeText"));
            vo.setNoticeType(request.getParameter("noticeType"));
            vo.setCreateDate(request.getParameter("createDate"));
            NoticeService noticeService = new NoticeServiceImpl();
            //调用Service层增加方法(add),增加记录
            noticeService.add(vo);
            this.redirectList(request, response);
        } else if ("delete".equals(action)) {//删除
            //取出表要删除的公告记录的主键
            long id = Long.parseLong(request.getParameter("id"));
            NoticeService noticeService = new NoticeServiceImpl();
            //调用Service层删除方法(delete),将对应的记录删除
            noticeService.delete(id);
            this.redirectList(request, response);
        } else if ("edit".equals(action)) {//修改
            //取出页面传进来的各个数据,并设置到Notice对象的属性里
            Notice vo = new Notice();
            vo.setId(Long.valueOf(request.getParameter("id")));
            vo.setNoticeName(request.getParameter("noticeName"));
            vo.setNoticeText(request.getParameter("noticeText"));
            vo.setNoticeType(request.getParameter("noticeType"));
            vo.setCreateDate(request.getParameter("createDate"));
            NoticeService noticeService = new NoticeServiceImpl();
            //调用Service层更新方法(update),更新记录
            noticeService.update(vo);
            this.redirectList(request, response);
        } else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
            Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
            NoticeService noticeService = new NoticeServiceImpl();
            Notice vo = noticeService.get(id);
            response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(vo));
            vo.setCheWei(request.getParameter("cheWei"));
            vo.setCheStatus(request.getParameter("cheStatus"));
            vo.setCheText(request.getParameter("cheText"));
            CheService cheService = new CheServiceImpl();
            //调用Service层更新方法(update),更新记录
            cheService.update(vo);
            this.redirectList(request, response);
        } else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
            Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
            CheService cheService = new CheServiceImpl();
            Che vo = cheService.get(id);
            response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(vo));
        } else {//默认去列表页面
            this.redirectList(request, response);
        }
    }

    /**
     * 处理Get请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
    }

    /**
     * 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
     *
     * @param request
     * @param response
     */
    private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //查询列和关键字
        String searchColumn = request.getParameter("searchColumn");
        String keyword = request.getParameter("keyword");
        Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
        params.put("searchColumn", searchColumn);//要查询的列
        params.put("keyword", keyword);//查询的关键字
        CheService cheService = new CheServiceImpl();
        Map<String, Object> map = cheService.list(params);
        request.getSession().setAttribute("list", map.get("list"));

        Integer totalRecord = (Integer) map.get("totalCount");//根据查询条件取出对应的总记录数,用于分页
        String pageNum = request.getParameter("pageNum");//封装分页参数
        com.demo.util.PageBean<Object> pb = new com.demo.util.PageBean(Integer.valueOf(pageNum != null ? pageNum : "1"), totalRecord);
        Random random = new Random();
        // 保存minColor最大不会超过255
        if (minColor > 255)
            minColor = 255;
        //  保存minColor最大不会超过255
        if (maxColor > 255)
            maxColor = 255;
        //  获得红色的随机颜色值
        int red = minColor + random.nextInt(maxColor - minColor);
        //  获得绿色的随机颜色值
        int green = minColor + random.nextInt(maxColor - minColor);
        //  获得蓝色的随机颜色值
        int blue = minColor + random.nextInt(maxColor - minColor);
        return new Color(red, green, blue);
    }
}
package com.demo.servlet;

/**
 * 单车模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用CheService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/CheServlet")
public class CheServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
            long id = Long.parseLong(request.getParameter("id"));
            XiuService xiuService = new XiuServiceImpl();
            //调用Service层删除方法(delete),将对应的记录删除
            xiuService.delete(id);
            this.redirectList(request, response);
        } else if ("edit".equals(action)) {//修改
            //取出页面传进来的各个数据,并设置到Xiu对象的属性里
            Xiu vo = new Xiu();
            vo.setId(Long.valueOf(request.getParameter("id")));
            vo.setXiuName(request.getParameter("xiuName"));
            vo.setXiuReason(request.getParameter("xiuReason"));
            vo.setXiuDate(request.getParameter("xiuDate"));
            vo.setXiuHandledate(request.getParameter("xiuHandledate"));
            vo.setXiuRen(request.getParameter("xiuRen"));
            vo.setXiuPhone(request.getParameter("xiuPhone"));
            vo.setXiuStatus(request.getParameter("xiuStatus"));
            vo.setXiuText(request.getParameter("xiuText"));
            XiuService xiuService = new XiuServiceImpl();
            //调用Service层更新方法(update),更新记录
            xiuService.update(vo);
            this.redirectList(request, response);
        } else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
            Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
            XiuService xiuService = new XiuServiceImpl();
            Xiu vo = xiuService.get(id);
            response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(vo));
        } else {//默认去列表页面
            this.redirectList(request, response);
        }
    }

    /**
     * 处理Get请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
    }

    /**
     * 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
     *
     * @param request
     * @param response
 */
//@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("add".equals(action)) {//增加
            User vo = new User();
            //取出页面传进来的各个数据,并设置到User对象的属性里
            vo.setUsername(request.getParameter("username"));
            vo.setPassword(request.getParameter("password"));
            vo.setRealName(request.getParameter("realName"));
            vo.setUserSex(request.getParameter("userSex"));
            vo.setUserPhone(request.getParameter("userPhone"));
            vo.setUserText(request.getParameter("userText"));
            vo.setUserType(request.getParameter("userType"));
            UserService userService = new UserServiceImpl();
            //调用Service层增加方法(add),增加记录
            userService.add(vo);
            this.redirectList(request, response);
        } else if ("delete".equals(action)) {//删除
            //取出表要删除的用户记录的主键
            long id = Long.parseLong(request.getParameter("id"));
            UserService userService = new UserServiceImpl();
            //调用Service层删除方法(delete),将对应的记录删除
            userService.delete(id);
            this.redirectList(request, response);
        } else if ("edit".equals(action)) {//修改
            //取出页面传进来的各个数据,并设置到User对象的属性里
            User vo = new User();
            vo.setId(Long.valueOf(request.getParameter("id")));
            vo.setUsername(request.getParameter("username"));
            vo.setPassword(request.getParameter("password"));
            vo.setRealName(request.getParameter("realName"));
            vo.setUserSex(request.getParameter("userSex"));
            vo.setUserPhone(request.getParameter("userPhone"));
            vo.setUserText(request.getParameter("userText"));
    /**
     * 处理Get请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
    }

    /**
     * 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
     *
     * @param request
     * @param response
     */
    private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //查询列和关键字
        String searchColumn = request.getParameter("searchColumn");
        String keyword = request.getParameter("keyword");
        Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
        params.put("searchColumn", searchColumn);//要查询的列
        params.put("keyword", keyword);//查询的关键字
        NoticeService noticeService = new NoticeServiceImpl();
        Map<String, Object> map = noticeService.list(params);
        request.getSession().setAttribute("list", map.get("list"));

        Integer totalRecord = (Integer) map.get("totalCount");//根据查询条件取出对应的总记录数,用于分页
        String pageNum = request.getParameter("pageNum");//封装分页参数
        com.demo.util.PageBean<Object> pb = new com.demo.util.PageBean(Integer.valueOf(pageNum != null ? pageNum : "1"), totalRecord);
        params.put("startIndex", pb.getStartIndex());
        params.put("pageSize", pb.getPageSize());
        List list = (List) noticeService.list(params).get("list");//根据分页参数startIndex、pageSize查询出来的最终结果list
        pb.setServlet("NoticeServlet");
        pb.setSearchColumn(searchColumn);
        pb.setKeyword(keyword);
        pb.setList(list);
        request.getSession().setAttribute("pageBean", pb);
        request.getSession().setAttribute("list", pb.getList());

        response.sendRedirect("notice_list.jsp");
    }
}
package com.demo.util;

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值