简体   繁体   中英

javascript - ajax - to close session on closing browser in JAVA

In my web application

1.1I have to close the session when the user abruptly close the browser.

1.2. When the user tries 'open in a new tab' - The user should be forwarded to the 'login page' in the new tab

I am planning to use JAVASCRIPT and AJAX to implement this in JSPX.

Can any one share any sample code to implement he above as I am new to javascript/ajax?

This can be achieved by adding the following META tags in the HEAD of your HTML. These tags will clear the cookies when the window is closed. hence the user need to relogin the next time.

<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>

1.1I have to close the session when the user abruptly close the browser.

There is no reliable way to track it, you could continuously poll to server using AJAX, and if you stop receiving the ajax call consider that browser has been closed

Or call a javascript method that signals your server about close, but this will not work on weird shutdown/crashes

1.2. When the user tries 'open in a new tab' - The user should be forwarded to the 'login page' in the new tab

If user is logged in, and you want to manage another session of browser's new tab, that is not possible, because browser manages cookies centrally (amongst all tabs and window). so when it sends the request it will add the cookie in the header so your application will not be able to identify,

If you manage session using other technique like jSessionId in url parameter it is possible to do but not a 100% sure way because user can play with sessionID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM