Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am assuming the existing session times out on the server before a request is received, and not because user logged out.</p> <p>Since the session will be deleted and its data lost, you have to make a copy of the session data in order to allow automatic "session continuation".</p> <p><strong>NOTE</strong> Untested code, just an idea.</p> <p>The idea is to watch for <code>HttpSessionListener.sessionDestroyed()</code> event:</p> <pre><code>class SessionDataBackupListener implements HttpSessionListener { private Map&lt;String,Object&gt; oldSessions = new HashMap&lt;String,Object&gt;(); public void sessionDestroyed(HttpSessionEvent event) { final Session s = event.getSession(); if (s.getAttribute("logOut")) { // Session was closed on-command, nothing to do. return; } // Session (probably) timed-out. Object sessionData = extractSessionData(s); // This should go into some kind of singleton DAO object. oldSessions.put(s.getAttribute("uniqueId"), sessionData); } } </code></pre> <p>Your log-out handler would look like:</p> <pre><code> public void logOut(HttpServletRequest req) { final Session s = req.getSession(); final String userId = s.getParameter("userId"); // Log the user out. // Mark the session was closed on-command. s.setAttribute("logOut", Boolean.TRUE); } </code></pre> <p>Your log-in handler would look like:</p> <pre><code> public void logIn(HttpServletRequest req) { final String uniqueId = s.getParameter("uniqueId"); Session s = req.getSession(false); // User returning after time-out? if (s == null &amp;&amp; uniqueId != null) { // Create new session... s = req.getSession(true); // ...assign it the same uniqueId... s.setAttribute("uniqueId", uniqueId); // ...and put back old session data. copyDataToSession(s, getOldSessionData(uniqueId)); } else { // User logging in as usual. } } </code></pre> <p><code>getOldSessionData(final String uniqueId)</code> would look up the singleton DAO object for earlier session.</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