[英]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.