Java 在不注销的情况下检索注销时间

Java 在不注销的情况下检索注销时间,java,session,servlets,httpsession,Java,Session,Servlets,Httpsession,当用户在未注销的情况下关闭浏览器时,如何查看注销时间 所以我在web.xml中实现了HttpSessionBindingListener,并添加了Listener,但在关闭浏览器后,注销时间并没有插入到数据库中。请问我哪里错了,有什么建议吗 logout.jsp <% ObjectWillBeInSession owi = new ObjectWillBeInSession(); owi.setProperty1("I am a value for Property1")

当用户在未注销的情况下关闭浏览器时,如何查看注销时间

所以我在web.xml中实现了
HttpSessionBindingListener
,并添加了
Listener,但在关闭浏览器后,注销时间并没有插入到数据库中。请问我哪里错了,有什么建议吗

logout.jsp

 <%
    ObjectWillBeInSession owi = new ObjectWillBeInSession();
    owi.setProperty1("I am a value for Property1");
    owi.setProperty2("I am a value for Property2");
    //this will call HttpSessionBindingListener's 
    //valueBound method for the object
    session.setAttribute("owi", owi);

    //this will call HttpSessionBindingListener's 
    //valueUnbound method for the object
    session.removeAttribute("owi");   
        //INSERT INTO DB.......BUT IT IS NOT WORKING
 %>

关闭浏览器不会触发对服务器的任何请求,因此您无法知道用户关闭了浏览器。
您可以使用侦听器为会话超时设置路径,然后在发生超时时存储当前时间。但是,会话通常在客户端发出最后一个请求数小时后过期。

您可以使用JavaScript的
窗口。onbeforeunload
向servlet发送AJAX请求,以记录用户关闭浏览器并在会话对象中执行所需操作的时间

session
在30分钟后失效,如
web.xml
中所示。如何在logout.jsp中知道此会话无效时间?您可以实现SessionListener()并存储调用
sessionDestroyed
时的当前时间。感谢Mathias的回答,我正在尝试实现您的方法一个选项是实现卸载事件处理程序,如[[1]中所述:onbeforeunload不仅会在浏览器关闭时被调用。感谢Behrooz的回答,但您能否在回答中再添加一点内容,以便我可以实现它,我应该如何使用此
窗口。onbeforeunload
?onbeforeunload不是非标准的吗?@MathiasSchwarz,我同意。但是
窗口。onbeforeunload
window.onunload
可能会有用。@Tom,可能会对您有所帮助。您需要根据Java世界进行定制。