简体   繁体   中英

Warn user before session time out in spring mvc

I have a web application implemented in Spring MVC, JSP having default timeout is 30 minutes.

I need to show alert in UI saying "Your session is going to end in 5 minutes. Please click OK to continue" if the session is going to expire in another 5 minutes.

How to achieve this in better way?

I found few answers here

Just interested to know if there is any other better way to do this.

try this out,below code works me fine

var cookieName = 'sessionMsg';
var message = 'Your session is Expires 5 min, click OK to continue';

function getCookie(name)
{
    var name = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++)
    {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) return c.substring(name.length,c.length);
    }
    return "";
}

function setSessionPrompt() {
    var timeout = getCookie(cookieName) - new Date().getTime();
    setTimeout(function(){
        if (new Date().getTime() < getCookie(cookieName)) {
            setSessionPrompt();
        } else {
            if(confirm(message)) {
                // do your action here
            }
        }
    }, timeout);
}

setSessionPrompt();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM