简体   繁体   English

如何在浏览器关闭或选项卡关闭时销毁 Vaadin 8 会话?

[英]How to destroy Vaadin 8 session on browser close or tab close?

How to destroy session on browser close/tab close in Vaadin?如何在 Vaadin 中的浏览器关闭/选项卡关闭时销毁会话? We can use javascript to handle this.我们可以使用 javascript 来处理这个问题。 I tried below code: Page.getCurrent().getJavaScript().execute("window.onbeforeunload = function (e) { var e = e || window.event; closeMyApplication(); return; };");我尝试了以下代码: Page.getCurrent().getJavaScript().execute("window.onbeforeunload = function (e) { var e = e || window.event; closeMyApplication(); return; };"); Problem with above script is, it will fire onbeforeunload event on page refresh also.上面脚本的问题是,它也会在页面刷新时触发 onbeforeunload 事件。 Also, how to handle this situation in mobile devices(touch devices)?另外,如何在移动设备(触摸设备)中处理这种情况?

I recommend to set closeIdleSessions to true and short heart beat interval, which should force Vaadin internal session clean up mechanism to destroy sessions faster after web browser has been closed.我建议将 closeIdleSessions 设置为 true 和短心跳间隔,这应该会强制 Vaadin 内部会话清理机制在 Web 浏览器关闭后更快地销毁会话。

If that is not helping, there is also helper add-on https://vaadin.com/directory/component/cleanupservlet-add-on如果这没有帮助,还有帮助器附加组件https://vaadin.com/directory/component/cleanupservlet-add-on

Copying from https://vaadin.com/docs/v8/framework/application/application-lifecycle.htmlhttps://vaadin.com/docs/v8/framework/application/application-lifecycle.html复制

Session Expiration会话到期

A session is kept alive by server requests caused by user interaction with the application as well as the heartbeat monitoring of the UIs.会话由用户与应用程序交互以及 UI 的心跳监控引起的服务器请求保持活动状态。 Once all UIs have expired, the session still remains.一旦所有 UI 都过期,会话仍然存在。 It is cleaned up from the server when the session timeout configured in the web application expires.当 Web 应用程序中配置的会话超时到期时,它会从服务器中清除。

If there are active UIs in an application, their heartbeat keeps the session alive indefinitely.如果应用程序中有活动的 UI,它们的心跳会无限期地使会话保持活动状态。 You may want to have the sessions timeout if the user is inactive long enough, which is the original purpose of the session timeout setting.如果用户不活动的时间足够长,您可能希望会话超时,这是会话超时设置的最初目的。 If the closeIdleSessions parameter of the servlet is set to true in the web.xml, as described in "Using a web.xml Deployment Descriptor", the session and all of its UIs are closed when the timeout specified by the session-timeout parameter of the servlet expires after the last non-heartbeat request.如果 servlet 的 closeIdleSessions 参数在 web.xml 中设置为 true,如“使用 web.xml 部署描述符”中所述,则会话及其所有 UI 将在会话超时参数指定的超时时关闭servlet 在最后一个非心跳请求后过期。 Once the session is gone, the browser will show an Out Of Sync error on the next server request.一旦会话消失,浏览器将在下一个服务器请求中显示 Out Of Sync 错误。 To avoid the ugly message, you may want to set a redirect URL for the UIs, as described in "Customizing System Messages".为了避免丑陋的消息,您可能需要为 UI 设置重定向 URL,如“自定义系统消息”中所述。

The related configuration parameters are described in "Other Servlet Configuration Parameters".相关的配置参数在“其他 Servlet 配置参数”中有描述。

You can handle session expiration on the server-side with a SessionDestroyListener, as described in User Session.您可以使用 SessionDestroyListener 在服务器端处理会话过期,如用户会话中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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