Note that there are some explanatory texts on larger screens.

plurals
  1. POUser closes a tab (implies logout) of many tabs, these are not redirected to index
    text
    copied!<p>I have the following problem</p> <p>I use a filter that enables user navigation to authorized pages, otherwise it derives to index.</p> <p>Obvious, but it must be said, One browser one session. Additionally, when the user closes the browser or tab his session expire.</p> <p>The problem arises when the user opens new tabs and he closes one of them, implying that the session is invalidated and the other tabs not redirect to index, because it has no ajax events.</p> <p>One of the solutions I tested.</p> <ul> <li>Only log off when only exist one tab, for that I will have to count the tabs in some a session variable or otherwise in the servlet's variable (<a href="https://stackoverflow.com/a/13717258/1303201">on this page I read this solution, but i did not understand this example ...</a>).</li> </ul> <blockquote> <p>This is the code that goes in the servlet, but does not explain how it is implemented.</p> </blockquote> <pre><code> public void trackUserTabs() { String onload = Controller.getParameter(ONLOAD_ID); if (onload != null &amp;&amp; onload.trim().equals("true")) { openedTabs++; System.err.println("onload: " + controller.getCurrentPrinciple() + "..........." + openedTabs); } String onunload = Controller.getParameter(ONUNLOAD_ID); if (onunload != null &amp;&amp; onunload.trim().equals("true")) { openedTabs--; System.err.println("onunload: " + controller.getCurrentPrinciple() + ".............." + openedTabs); } if (openedTabs &lt;= 0 &amp;&amp; controller.getCurrentProfile() != null) { /** * All tabs are closed, log out current user. */ controller.logoutCurrentProfile(); } </code></pre> <blockquote> <p>But it have a detail, I may not tell when the user closes the browser or tab.</p> </blockquote> <p>Another solution.</p> <ul> <li><p>Using a poll to call a function in javascript and ask if this logged. Try using this function but had no positive results.</p> <pre><code>function ComprobarSessionExpirada() { var request = false; if(window.XMLHttpRequest) { // Mozilla/Safari request = new XMLHttpRequest(); } else if(window.ActiveXObject) { // IE request = new ActiveXObject("Microsoft.XMLHTTP"); } var url = '/ACP_3.0/pag/resumen.jsf'; request.open('POST', url, true); request.onreadystatechange = function() { alert('a'); if(request.readyState == 4) { var session = eval('(' + request.responseText + ')'); if(session.valid) { alert('ok'); // DO SOMETHING IF SESSION IS VALID } else { alert('Your Session has expired'); window.location = '/ACP_3.0/index.jsf'; } } } request.send(null); } </code></pre></li> </ul> <p><strong>EDIT:</strong></p> <blockquote> <p>I forgot to mention that I maintain user information in a bean of application, this allows know the status of the user for the chat I did.</p> <p>So, you need to identify when the user closes the session directly (logout) or indirectly (close your browser or timeout), because if it does not close the session, the status of the user always stay online. </p> <p>To identify when he log off, I use a prelude to the destruction of session. Implement HttpSessionListener where the method sessionDestroyed change the user's status in the application bean.</p> </blockquote>
 

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