【发布时间】:2015-06-18 10:42:18
【问题描述】:
我想在浏览器关闭或选项卡关闭后终止会话。并且我需要在会话过期后使用 Session Listener 进行一次数据库连接。但是,为此我需要等到会话销毁。
这是会话销毁时执行的代码。
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);
}
但是,我不想等到会话销毁。我需要在浏览器关闭后执行此代码。希望有人能帮我解决这个问题。
谢谢
【问题讨论】:
标签: java javascript session