JAVA JSP幼儿园事务管理系统JSP早教中心网站系统 JSP幼儿园网站JSP幼儿园管理系统
开发语言:Java
开发工具:Myeclispe /Eclipse
数据库:MYSQL
应用服务:Tomcat
/** * @Author yy * @Description 登录 * @Date 2022.2.17 */ public class LoginController extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { JSONObject jsonObject = new JSONObject(); String username = req.getParameter("username"); String password = req.getParameter("password"); resp.setCharacterEncoding("UTF-8"); HttpSession session = req.getSession(); if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) { jsonObject.put("code", 2000); jsonObject.put("flag", "fail"); jsonObject.put("user", null); jsonObject.put("msg", "usernameOrPasswordIsBank");//用户名密码不能为空 resp.getWriter().print(jsonObject); return; } password = MyMD5Util.encrypt(password); System.out.println(password); BusinessUserVO businessUserVO = new BusinessUserVO(); businessUserVO.setUsername(username); businessUserVO.setPassword(password); StudentUserVO studentUserVO = new StudentUserVO(); studentUserVO.setUsername(username); studentUserVO.setPassword(password); String flag1 = null; String flag2 = null; try { flag1 = BusinessUserDao.selectUsername(businessUserVO); if ("ok".equals(flag1)) {//企业用户名存在 BusinessUserDTO businessUserDTO = BusinessUserDao.select(businessUserVO); if (businessUserDTO != null) { jsonObject.put("code", 2000); jsonObject.put("flag", "success");//登录成功 jsonObject.put("user", businessUserDTO); jsonObject.put("msg", "login_success"); session.setAttribute("businessUser",businessUserDTO); resp.getWriter().print(jsonObject); return; } else { jsonObject.put("code", 2000); jsonObject.put("flag", "fail");//登录失败 jsonObject.put("user", null); jsonObject.put("msg", "passwordError");//密码错误 resp.getWriter().print(jsonObject); return; } } flag2 = StudentUserDao.selectUsername(studentUserVO); if ("ok".equals(flag2)) {//学生用户名存在 StudentUser studentUser = StudentUserDao.select(studentUserVO); if (studentUser != null) { jsonObject.put("code", 2000); jsonObject.put("flag", "success");//登录成功 jsonObject.put("user", studentUser); jsonObject.put("msg", "login_success"); session.setAttribute("studentUser",studentUser); resp.getWriter().print(jsonObject); return; } else { jsonObject.put("code", 2000); jsonObject.put("flag", "fail");//登录失败 jsonObject.put("user", null); jsonObject.put("msg", "passwordError");//密码错误 resp.getWriter().print(jsonObject); return; } } //用户名不存在,前往注册 jsonObject.put("code", 2000); jsonObject.put("flag", "fail");//登录失败 jsonObject.put("user", null); jsonObject.put("msg", "usernameIsNotExist");//密码错误 resp.getWriter().print(jsonObject); return; } catch (SQLException throwables) { throwables.printStackTrace(); } return; } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { doGet(req, resp); } } 管理员登录控制层: public class AdminLoginController extends HttpServlet { @SneakyThrows @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String password = req.getParameter("password"); password = MyMD5Util.encrypt(password); JSONObject jsonObject = new JSONObject(); HttpSession session = req.getSession(); Admin admin = new Admin(username, password); Admin adminFromDB = AdminDao.findByUsernamePassword(admin); if (adminFromDB!=null){ jsonObject.put("code",2000); jsonObject.put("msg","login_success"); jsonObject.put("admin",adminFromDB.getUsername()); jsonObject.put("flag","success"); resp.getWriter().print(jsonObject); session.setAttribute("admin",adminFromDB); return; }else { jsonObject.put("code",2000); jsonObject.put("msg","no admin"); jsonObject.put("admin",null); jsonObject.put("flag","fail"); resp.getWriter().print(jsonObject); return; } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }