Skip to content Skip to sidebar Skip to footer

Checkout When You Close The Tab

In my application with Java EE and JSF 2.0 I would like that when the user closes the tab of the application the session ends. If this user has opened another tab in the applicatio

Solution 1:

This isn't possible the way you want. All windows/tabs of the same browser instance share the same session.

To achieve this anyway, you need to create a single view with conditionally rendered includes and a view scoped bean wherein you store everything related to the view and always return void or null from action methods. This way you don't need the session scope at all. To create a new view, just send a new GET request (like a new tab) or return a non-null string from action method.

Solution 2:

Probably this is too late, but since i have seen a number of questions on the web pertaining to this, i may just put my solution here, that i have tested on chrome and firefox (all latest editions though)

On set: a more elegant solution is to use a filter, but i have used a prerender event (Using single template for my entire website, hence i can track when every page is loaded, and i have specified the prerender event on the template)

Javascript section:

window.onbeforeunload = function(){
      //log out of the server too.var xmlhttp;
      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=newXMLHttpRequest();
      }
      else
      {// code for IE6, IE5
        xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
      }
      var url = '/context/index.xhtml?onunload=true';
      xmlhttp.open('GET', url, true);
      xmlhttp.send();
    }
     window.onload = function(){
      console.log('window name: '+window.name);
      if(window.name != '#{controller.sessionId}'){
        window.name = '#{controller.sessionId}';
        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=newXMLHttpRequest();
        }
        else
        {// code for IE6, IE5
          xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
        }
        var url = '/context/index.xhtml?onload=true';
        xmlhttp.open('GET', url, true);
        xmlhttp.send();
      }
    }
    //Called when user logs out.functiononLogout(){
      console.log('logging out: '+window.name);
      window.name = '';
      console.log('logged out: '+window.name);
    }

Server Side Code:

This method is called before the view is rendered on the prerender event

publicvoidtrackUserTabs() {
String onload = Controller.getParameter(ONLOAD_ID);
if (onload != null && onload.trim().equals("true")) {
  openedTabs++;
  System.err.println("onload: " + controller.getCurrentPrinciple() + "..........." + openedTabs);
}
String onunload = Controller.getParameter(ONUNLOAD_ID);
if (onunload != null && onunload.trim().equals("true")) {
  openedTabs--;
  System.err.println("onunload: " + controller.getCurrentPrinciple() + ".............." + openedTabs);
}
if (openedTabs <= 0 && controller.getCurrentProfile() != null) {
  /**
   * All tabs are closed, log out current user.
   */
  controller.logoutCurrentProfile();
}

}

The javascript code for ajax request is courtesy of an answer in Stackoverflow.

Post a Comment for "Checkout When You Close The Tab"