【问题标题】:Increase session some value增加会话一些价值
【发布时间】: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代码有什么问题?

【问题讨论】:

    标签: java html jsp session


    【解决方案1】:

    因为你每次都放1,所以你需要读取它的值然后递增

    改变

    int i = 0;
    

    int i = Integer.parseInt(session.getAttribute("tryTimes") == null ? "0" : session.getAttribute("tryTimes"));
    

    然后在 JSP 上你有 JOptionPane 这没有任何意义,你需要为它生成 HTML

    同样在 JSP 上,您没有将会话属性读入 tryTimes

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      相关资源
      最近更新 更多