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 use开发者_StackOverflowr? 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>
精彩评论