比较新旧会话

时间:2017-08-15 02:39:09

标签: java jsp servlets

对于一个实例,我们有登录页面。我们正在创建会话并将其文本附加到它..我正在尝试比较旧会话和新会话。让我们来吧,

1)用户登录测试//如果密码不正确,它将重定向回登录页面并附加" 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();
            }

    }

1 个答案:

答案 0 :(得分:0)

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

所以,你不可能以这种方式实现你想要的。您需要将数据存储在其他位置,例如数据库或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
}
相关问题