private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String verifycode = request.getParameter("verifycode"); String machinecode = (String) request.getSession().getAttribute("rand"); if (verifycode.equalsIgnoreCase(machinecode)) { // 2.验证用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username); User user = userService.login(username, password); if (user != null) { // 登录成功 request.getSession().setAttribute("user", user); response.sendRedirect("index.jsp"); } else { // 登录失败 request.setAttribute("msg", "用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request, response); } } else { request.setAttribute("msg", "验证码错误"); request.getRequestDispatcher("login.jsp") .forward(request, response); } } private void login2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String verifycode = request.getParameter("verifycode"); String machinecode = (String) request.getSession().getAttribute("rand"); if (verifycode.equalsIgnoreCase(machinecode)) { // 2.验证用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username); User user = userService.login(username, password); if (user != null) { // 登录成功 request.getSession().setAttribute("user", user); PrintWriter out = response.getWriter(); out.write(JSON.toJSONString(new MsgVo(1,"登陆成功"))); } else { // 登录失败 PrintWriter out = response.getWriter(); out.write(JSON.toJSONString(new MsgVo(2,"用户名和密码错误"))); } } else { PrintWriter out = response.getWriter(); out.write(JSON.toJSONString(new MsgVo(3,"验证码错误"))); } }