简体   繁体   中英

Session Destroyed; Print Message in JSP and Servlets

When a session has been destroyed, how do I print a message on a JSP that notifies the user? I'm using a class that implements HttpSessionListener.

When the session is destroyed, you can't do anything from the server side on anyway. At the point of session destroy there is no guarantee that you have valid request/response objects at your hands. Your best bet is to handle it fully at the client side, using for example JS. You can get the remaining lifetime of the current session by HttpSession#getMaxInactiveInterval() and you can use JavaScript's setTimeout() to run a function after some time.

<script>
    setTimeout(function() {
        document.getElementById('message').innerHTML = 'Session has timed out!';
    }, ${pageContext.session.maxInactiveInterval} * 1000); // It returns seconds, but setTimeout expects milliseconds.
</script>
<div id="message"></div>

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