login.jsp页面表单
login.jsp页面js
function formSubmit() { var staffName = document.oForm.staffName.value; var password = document.oForm.password.value; if(staffName=="" || password ==""){ alert("登陆账号和密码不能为空"); return false; } //异步登录验证 $.ajax({ url:"login.do?method=check&staffName="+staffName+"&password="+password+"&" + "rd="+Math.random(), type:"post", success: function(response){ if(response=="false"){ alert("您输入的帐号或密码错误!"); return false; } if(response=="true"){ document.oForm.submit(); } } });}
//loginController.java
public ModelAndView check(HttpServletRequest request ,HttpServletResponse response) throws Exception{ request.getSession().removeAttribute(STAFF_SESSION_NAME); PrintWriter out = response.getWriter(); String staffName = request.getParameter("staffName"); String password = request.getParameter("password"); String hql = "FROM Staff WHERE loginName='"+StringUtils.sqlFormat(staffName)+"' AND loginPwd='"+StringUtils.sqlFormat(password)+"'"; ListstaffList = staffDao.find(hql); if (staffList == null || staffList.size() == 0) { out.print("false"); return null; }else{ out.print("true"); return null; } }