My requirement is to notify the user with a popup saying that the user session is about to time out in x seconds in case user does not perform any activity on the web page.
Addition to this requirement is to decrement the value x seconds dynamically in the popup.
The environment I use is Java EE.
Make use of
HttpSession#getMaxInactiveInterval()andsetTimeout(). There’s no need for Ajax in this particular purpose, unless you want to postpone the timeout on every client activity (polling).Basic example:
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 anothersetTimeout()on 1 second to change the text dynamically.Note that this script has to be served by the
JspServletto 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*.jsfile, then you need to let theJspServlethandle any*.jsrequests as well.