Make use of HttpSession#getMaxInactiveInterval()
and setTimeout()
. There's no need for Ajax in this particular purpose, unless you want to postpone the timeout on every client activity (polling).
Basic example:
<script>
var secondsBeforeExpire = ${pageContext.session.maxInactiveInterval};
var timeToDecide = 15; // Give client 15 seconds to choose.
setTimeout(function() {
alert('Your session is about to timeout in ' + timeToDecide + ' seconds!')
}, (secondsBeforeExpire - timeToDecide) * 1000);
</script>
To decrement the time inside the message magically, then instead of the basic alert()
you'll need an overlay with a div wherein you have control over the content through HTML DOM tree and make use of another setTimeout()
on 1 second to change the text dynamically.
Note that this script has to be served by the JspServlet
to get the EL to work. Thus, you need to put the script in the HTML <head>
of the JSP page, or if you really want to have all the JS in a separate *.js
file, then you need to let the JspServlet
handle any *.js
requests as well.