会话到期后页面不重定向

时间:2013-05-20 09:51:54

标签: java jsp session servlets

我已经做了一个简单的页面,在会话到期时移动到另一个页面。我已将时间设置为50秒,之后它将移动到页面但不幸的是它没有移动,我无法知道哪里出错了。如果你能解决我的问题,我将非常感激

function x(){
       var timeOut =<%=session.getAttribute("login")%>;

var checkTimeout;

checkTimeOut = function(){
    if(timeOut==null || timeOut==""){
        window.location.replace("failedSession.jsp");
//document.getElementById("b").innerHTML="session timed out";
        // redirect to timeout page
    }else{


        window.setTimeout(checkTimeOut, 1000); // check once per second
    }}
checkTimeOut(); // this is where you insert checkTimeOut function so that when you call x(), this will execute in the end.
}

2 个答案:

答案 0 :(得分:0)

您粘贴的代码似乎最初没有调用checktimeout函数。这也适用于您修改后的代码。它只定义了函数checktimeout()。

我会在body标签上添加一个onload函数调用,它会在第一次调用checktime时。

function startTimeoutCheck() {
    window.setTimeout(checkTimeout, 1000);
}

<body onload="startTimeoutCheck()">
    ....
</body>

checktimout变量显然应该在上述函数的范围(全局)中。

答案 1 :(得分:0)

您的代码存在问题:

在您的代码中,您正在调用checkTimeOut();。这就是它没有被执行的原因。您应该通过将alert放在函数内来检查函数的执行情况。可能会在body onload期间@rgeorge提出建议。

注意:您的逻辑看起来没问题。还有习惯在javascript 语句之后添加分号{/ 1}}。

答案已更新:

1)请勿使用@Markfunction name名称相同。例如:variable。名字独特。这是相互矛盾的。

2)您的x始终大于x因为lastActivity + timeOut未增加(7> 1 + 1)。这意味着x条件永远不会执行。这意味着永远不会调用else

3)您的函数checkTimeOut本身永远不会执行,因为它永远不会从任何地方调用。您只是执行checkTimeOut()。这不足以致电x()

答案已更新:

这样做:

checkTimeOut()

您的代码适用于我。 <script type="text/javascript"> function x(){ var timeOut =1; var lastActivity = 1; var x=7; var checkTimeout; checkTimeOut = function(){ if(x > lastActivity + timeOut){ document.getElementById("b").innerHTML=x; // redirect to timeout page }else{ window.setTimeout(checkTimeOut, 1000); // check once per second }} checkTimeOut(); // this is where you insert checkTimeOut function so that when you call x(), this will execute in the end. } </script> 值更改为DIV。这就是您的代码所做的&gt;&gt; http://jsfiddle.net/hmHP9/

相关问题