【问题标题】:Comparing new and old session比较新旧会话
【发布时间】:2017-08-15 13:38:28
【问题描述】:

例如,我们有登录页面。我们正在创建会话并将我们的文本附加到它。我正在尝试比较旧会话和新会话。让我们说,

1) 用户使用 test 登录 // 如果密码不正确,它将重定向回登录页面并将“test”附加到会话

2) 但是如果用户在第二次尝试时使用不同的用户名登录 // 我想将其附加到新会话并与旧会话进行比较。 让我们说,,

if(session1 is not equal to session 2){
   restart attempt incremental
} else if (session1 is to session 1){
  increment attempt to -1
}

让我们这样说 我们的jsp文件如下

<form action="servlet" method="post">
<input type="text" name="username">
</form>

在我们的 servlet 文件中

doPost(HttpSe....){
//Get Parameter 
String name = request.getParameter("username");

//Create session
HttpSession session = request.getSession(true);
session.setAttribute("username", name);
String session_name = (String)session.getParameter("username");

//validate if username is = test, it will redirect

if(name.equal("test")){
  //this will redirect to different page
} else if(session_name.equals(name)){
  //do something..
} else {
 //do something...
}

我只需要比较旧会话和新会话。

我尝试使用 (session.isNew()) 但它不起作用,它总是会将我重定向到 else 语句..

这是我的实际代码

HttpSession session = request.getSession(true);
        HttpSession session_Newusername = request.getSession(true);
        session.setAttribute("Username", username);
        String session_Username = (String)session.getAttribute("Username");
        System.out.println(" data .... " + session_Username);

        int loginAttempt;
            if(session.getAttribute("loginCount") == null){
                session.setAttribute("loginCount", 0);
                loginAttempt = 0;
            } else if(session.isNew()) {
                System.out.println("If user key in same username");
                loginAttempt = (Integer)session.getAttribute("loginCount");
                if(loginAttempt  == 3){ 
                    session.setMaxInactiveInterval(5);
                    session.setAttribute("message", "Your account is blocked, Please contact admin or wait for 2mins");
                    //System.out.println(session.getAttribute("message"));
                    response.sendRedirect("Pages/Login/login.jsp");
                } else {
                    System.out.println("How many attempt left " + loginAttempt);
                    loginAttempt++;
                    int leftAttempt = 4 - loginAttempt;
                    session.setAttribute("message", "You are left with " + leftAttempt + " attempt");
                    //System.out.println(session.getAttribute("message"));
                    response.sendRedirect("Pages/Login/login.jsp");
                }
                session.setAttribute("loginCount", loginAttempt);
            } else {
                response.sendRedirect("Pages/Login/login.jsp");
                System.out.println("username is not the same");
                session.invalidate();
            }

    }

【问题讨论】:

  • 那么你的问题是什么?
  • 我想在新会话中存储新值并与同一会话中的旧值进行比较
  • 交换值,以便您可以轻松存储新值并将其与其他值进行比较。
  • request.getSession(true); 只会在新会话不存在的情况下返回一个新会话 - 因此您的两个会话变量都指向同一个会话

标签: java jsp servlets


【解决方案1】:

当前一个会话无效(例如超时后关闭)时,会话属性将被销毁。因此,您不能添加这些属性并尝试从先前已经失效的会话中获取它。您只能在同一个活动会话中访问它们。

因此,您无法以这种方式实现您想要的。您需要将数据存储在其他地方,例如数据库或 JVM 内存中。但不在会话属性中。

如果要确定在同一个活动会话中,则需要检查当前会话中是否存在userName 属性,然后根据需要进行操作:

userName = (String) session.getAttribute("userName");
if (userName == null) {
    // here you don't have it from previous invocation of setAttribute
    session.setAttribute("userName", login);
} else {
    // you already setAttribute from previous invocation on current session
    // add you errors here
}

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2012-09-30
    • 1970-01-01
    相关资源
    最近更新 更多