【发布时间】:2015-02-26 21:57:36
【问题描述】:
我想知道一个用户是否有3次登录失败,然后出现Help链接。
所以我需要在会话中存储一些值并在每次用户登录时检查其值(称为tryTimes):
我在doLoginservlet 中的代码:
if (logedInSuccessfully()) { // OK
//create session and add sum attributes
response.sendRedirect("Home.jsp");
} else {
int i = 0;
HttpSession session = request.getSession(true);
session.setAttribute("existsInDB", "No");
session.setAttribute("tryTimes", ++i);
response.sendRedirect("Login.jsp"); // back to log in page again
}
在Login.jsp 页面:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title> Login Page </title>
</head>
<body>
<%
int tryTimes = 0;
if (String.valueOf(session.getAttribute("existsInDB")).equalsIgnoreCase("No")) {
JOptionPane.showMessageDialog(null, "No: " + session.getAttribute("tryTimes"));
if (tryTimes >= 3) {
%>
<a href="LoginHelp.jsp"> <font color="white"> Need Help? </font> </a>
<%
}
}
%>
...
//End of page
但是,当我测试i 的值时,它始终是1 并且根本没有变化。
m代码有什么问题?
【问题讨论】: