Ana*_*mar 6 javascript java session
我想在浏览器关闭或标签关闭后终止会话.我需要在会话到期后使用Session Listener.But进行一次数据库连接,为此我需要等到会话销毁.
这是会话销毁时执行的代码.
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);
}
Run Code Online (Sandbox Code Playgroud)
但是,我不想等到会话破坏.我需要在浏览器关闭后执行此代码.希望有人能让我离开这个.
谢谢
如果你使用jquery你可以监听该unload事件
https://api.jquery.com/unload/
或者使用JS
window.onbeforeunload = function() {...
Run Code Online (Sandbox Code Playgroud)
当然,您可以触发一些 ajax 来调用服务器端代码。
| 归档时间: |
|
| 查看次数: |
6312 次 |
| 最近记录: |