简体   繁体   English

我需要杀死会话并在浏览器关闭或制表符关闭后执行一个功能

[英]I need to kill session and do one function after browser close or tab close

I want to kill session after browser close or tab close.And I need to do one database connectivity after the session get expires using Session Listener.But, for that I need to wait until the session destroys. 我想在浏览器关闭或标签关闭后终止会话。我需要在会话到期后使用Session Listener.But进行一次数据库连接,为此我需要等到会话销毁。

Here is the code which executes when the session destroyed. 这是会话销毁时执行的代码。

public void sessionDestroyed(HttpSessionEvent event) {
        synchronized (this) {
            //System.out.println("deletion");
            ServletContext application = event.getSession().getServletContext();
            sessionCount = (Integer) application.getAttribute("SESSION_COUNT");
            application.setAttribute("SESSION_COUNT", sessionCount=sessionCount - 1);
            //application.setAttribute("SESSION_COUNT", --sessionCount);


            try
            {
                Class.forName("com.mysql.jdbc.Driver");
            }
            catch (ClassNotFoundException e) {
                System.out.println("" + e);
            }

            Connection connection = null;
            try {
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/raptor1_5", "root", "");
                Statement st = connection.createStatement();

                st.executeUpdate("update adminlogin set Password='admin' where Username='admin'");  

            }   catch (SQLException e) {
                e.printStackTrace();
            }

        }
        System.out.println("Session Destroyed: " + event.getSession().getId());
        System.out.println("Total Sessions after delete: " + sessionCount);
    }

But, I don't want to wait until the session destroys.I need to do this code after the browser gets close.Hope someone will get me out of this. 但是,我不想等到会话破坏。我需要在浏览器关闭后执行此代码。希望有人能让我离开这个。

Thank you 谢谢

If you use jquery you can listen for the unload event 如果使用jquery ,则可以侦听unload事件

https://api.jquery.com/unload/ https://api.jquery.com/unload/

or using JS 或使用JS

 window.onbeforeunload = function() {...

Then of couse you could fire some ajax to call the server side code. 然后你可以发一些ajax来调用服务器端代码。

Detect the Browser's Close Event and Invalidate Session using 使用检测浏览器的 Close Event和使会话无效

if(session!=null) { 
session.invalidate();   
}   

How to detect browser Close Event ? 如何检测浏览器关闭事件?

$(document).ready(function()
{
 $(window).bind("beforeunload", function() { 
   return confirm("Do you really want to close?"); // here you can invalidate
 });
});

Update 更新

How to differentiate between Browser Close and Refresh Event ?? 如何区分浏览器关闭和刷新事件?

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

相关问题 关闭Reader后是否需要关闭InputStream? - Do I need to close InputStream after I close the Reader 如果我关闭ObjectOuputStream,那么是否不需要关闭FileOutputStream? - If I close ObjectOuputStream, then do not need to close FileOutputStream? 浏览器关闭后如何保留会话? - How to retain session after browser close? 如何在浏览器选项卡中检测到关闭? - How can I detect a close in the browser tab? 我是否需要关闭ByteArrayInputStream? - Do I need to close a ByteArrayInputStream? 我需要关闭流吗? - Do I need to close a stream? 执行#isClosed方法后是否需要关闭连接? - Do I need to close the connection after doing #isClosed method? 使用 getResponseCode (HttpUrlConnection) 后是否需要关闭 inputStream - Do I need close inputStream after using getResponseCode (HttpUrlConnection) 我是否需要处理或忽略OutputStream close()函数触发的IOException? - Do i need to handle or ignore the IOException fired by OutputStream close() function? Google App Engine Java-用户关闭浏览器后使会话保持活动状态(记住我的功能) - Google App Engine Java - Keep Session alive after users close browser (remember me function)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM