Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create an activity checker which checks every minute if any user activity has taken place (mouseclick, keypress) and performs a heartbeat to the server side to keep the session alive when the user is active and does nothing when the user is not active. When there is no activity for 30 minutes (or whatever default session timeout is been set on server side), then perform a redirect.</p> <p>Here's a kickoff example with little help of <a href="http://jquery.com" rel="noreferrer">jQuery</a> to bind click and keypress events and fire ajax request.</p> <pre class="lang-html prettyprint-override"><code>&lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function() { $.active = false; $('body').bind('click keypress', function() { $.active = true; }); checkActivity(1800000, 60000, 0); // timeout = 30 minutes, interval = 1 minute. }); function checkActivity(timeout, interval, elapsed) { if ($.active) { elapsed = 0; $.active = false; $.get('heartbeat'); } if (elapsed &lt; timeout) { elapsed += interval; setTimeout(function() { checkActivity(timeout, interval, elapsed); }, interval); } else { window.location = 'http://example.com/expired'; // Redirect to "session expired" page. } } &lt;/script&gt; </code></pre> <p>Create a <code>Servlet</code> which listens on <code>/heartbeat</code> and does basically just the following:</p> <pre><code>@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { request.getSession(); } </code></pre> <p>to keep the session alive.</p> <p>When you store the logged-in user in the session, it will be "automagically" logged out whenever the session expires. So you don't need to manually logout the user.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload