admin管理员组文章数量:1334887
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.
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(".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
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.
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(".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
Share Improve this question asked Apr 13, 2015 at 7:27 AnandKumar SelvamaniAnandKumar Selvamani 3991 gold badge8 silver badges22 bronze badges2 Answers
Reset to default 2 +50If you use jquery
you can listen for the unload
event
https://api.jquery./unload/
or using JS
window.onbeforeunload = function() {...
Then of couse you could fire some ajax to call the server side code.
Detect the Browser's Close Event
and Invalidate Session using
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 ??
本文标签: javaI need to kill session and do one function after browser close or tab closeStack Overflow
版权声明:本文标题:java - I need to kill session and do one function after browser close or tab close - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742335403a2455522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论