Warn the user before session timeout

java, javascript, jquery, session

Solution

Try this code. Hopefully it will solve your problem. Thanks

var cookieName = 'sessionMsg';
var message = 'Your session is going to be end by 5 min, Please 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();

Problem

I have a web application implemented in `Spring MVC`, `JSP`. Default session timeout is defined in `web.xml` is 30 min. if user is idle for 25 mins than I need to show a popup to user with message that `Your session is going to be end by 5 min, Please click OK to continue`. Do we can achieve this using `JavaScript`, `jquery` or any other approach? Please suggest.

Original source