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 application then the session of this other tab should not be closed. This so to not disturb the management of the application.

To end the session I use:

public String deconnecter() { //invalidate user session FacesContext context = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) context.getExternalContext().getSession(false); session.invalidate(); return "login"; }


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.


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=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("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=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } var url = '/context/index.xhtml?onload=true'; xmlhttp.open('GET', url, true); xmlhttp.send(); } } //Called when user logs out. function onLogout(){ 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

public void trackUserTabs() { 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.


  • Anybody have any idea how the divs are emitted in pinterest.com?
  • Setting color in a row of a Jtable
  • Conditional render in tagfile depending on whether the attribute is specified or not
  • Issue with routerLink directive
  • Using $compile in a directive triggers AngularJS infinite digest error
  • Updating Dojo provide
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Implementing “partial void” in VB
  • Sending HTML Form Data to Spring REST Web Service
  • Is it possible to access block's scope in method?
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Updating server-side rendering client-side
  • Trying to switch camera back to front but getting exception
  • How to set my toolbar fixed while scrolling android
  • Symfony2: How to get request parameter
  • what is the difference between the asp.net mvc application and asp.net web application
  • How to pass list parameters for each object using Spring MVC?
  • AT Commands to Send SMS not working in Windows 8.1
  • bootstrap to use multiple ng-app
  • Cannot Parse HTML Data Using Android / JSOUP
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • log4net write single file for each call to log.info
  • Free memory of cv::Mat loaded using FileStorage API
  • C# - Getting references of reference
  • Getting error when using KSoap library to consume .NET web services
  • Angular 2 constructor injection vs direct access
  • LevelDB C iterator
  • apache spark aggregate function using min value
  • EntityFramework adding new object to nested object collection
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • Sorting a 2D array using the second column C++
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?