-1
我一直在面对这个问题,我知道这是一个经常被问到的问题,但问题还没有被我的研究纠正。Servlet:登录尝试失败
我面临的问题是我创建了逻辑,如果相同的用户名将尝试登录2次,它应该减少尝试次数。但是,它总是停留在没有尝试2.
我基本上做了这个:我试图使用HttpSession来计数在页面上的访问次数,但失败。因为我觉得,每当我将其重定向到我的登录页面。它只会重新启动会话。
我的初步计划是,在我的JDBC代码中,如果用户尝试登录超过3次,它将阻止user_acc == 1
。但是看到我面临的主要问题,我决定使用基本逻辑,只需从输入字段输入用户名,而无需连接到数据库。在我纠正这个问题之后,我会修改它以将它连接到我的数据库。
任何帮助将真正被赞赏。
String name = request.getParameter("username");
String pass = request.getParameter("password");
PrintWriter pw = response.getWriter();
int login_attempt = 3;
HttpSession session=request.getSession();
session.setAttribute("name",name);
String sessionName = (String) session.getAttribute("name");
if(name.equals("scott")){
if(pass.equals("abc"))
{
pw.println(("Success"));
pw.println(sessionName);
}
else {
login_attempt = login_attempt - 1;
if(login_attempt < 3 && sessionName.equals(name)){
pw.println(" You have this try " + login_attempt + " : " + sessionName);
} else if(login_attempt == 0 && sessionName.equals(name)){
pw.println("Please contact admin for as your account is blocked");
}
}
}
解决 为他人谋取利益,我已经解决了这个问题
int loginAttempt;
if (session.getAttribute("loginCount") == null) {
// This will first set login count to zero
session.setAttribute("loginCount", 0);
loginAttempt = 0;
} else {
loginAttempt = (Integer) session.getAttribute("loginCount");
// System.out.println("How many attempt " + loginAttempt);
}
if (loginAttempt == 3) {
session.setMaxInactiveInterval(5);
System.out.println(("game over"));
pw.println("No more attempt please call admin\n\n");
pw.println("<br/> \n\n\n\n\n\n left for interval");
} else {
System.out.println("How many attempt " + loginAttempt);
// loginAttempt = loginAttempt + 1;
loginAttempt++;
int allowLogin = 3 - loginAttempt;
session.setAttribute("message", "loginAttempt= " + loginAttempt
+ ". Invalid username or password. You have " + allowLogin + " attempts remaining.");
System.out.println(session.getAttribute("message"));
response.sendRedirect("login.jsp");
}
if (isTrue != false) {
pw.println("Username does not exist in database");
}
session.setAttribute("loginCount", loginAttempt);
pw.println("Invalid users");
这代码不完整。我想是在某个班级。变量login_attempt是本地的,每次拍摄都会被还原 –
我不完全明白它是如何比较用户名以将其存储为会话,以及何时将其重定向到具有不正确密码的登录页面。它会根据登录失败识别用户名并相应地减少尝试次数。任何想法? –
如果你解决了你自己的问题,那么你应该把它作为**答案**,不要将它编辑成你的问题。请从您的问题中删除解决方案,并将其作为答案发布。 –