我正在尝试限制用户可以尝试使用servlet登录的尝试次数,但是3次尝试后它不会显示“被阻止的帐户”。我该如何解决?请帮助。
这是我的代码:
int maxAttempts;
maxAttempts = 0;
while(maxAttempts < 3)
{
String username = request.getParameter("username");
String password = request.getParameter("password");
HttpSession session = request.getSession();
if(LoginConnection.checkUser(username, password))
{
session.setAttribute("username", username);
RequestDispatcher rs = request.getRequestDispatcher("profile.jsp");
rs.forward(request, response);
break;
}
else
{
String error = "Invalid password";
request.setAttribute("error", error);
request.getRequestDispatcher("index.jsp").forward(request, response);
maxAttempts++;
break;
}
} if(maxAttempts == 3) {
String error = "Blocked account.";
request.setAttribute("error", error);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
您需要在会话中进行最大尝试。不要使用一会儿。只有在其他情况下。将最后一个if块放在else中
如果if块从会话获取最大尝试次数,并对其进行递增,然后将其放回if块中的会话中,则>]