我一直面临着这个问题,我知道这是一个经常被问到的问题,但问题并没有得到我所做的研究的纠正。
我面临的问题是我创建了逻辑,如果相同的用户名将尝试登录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");
答案 0 :(得分:1)
已解决为了他人的利益,我已经解决了这个问题
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");