java - 当用户 session 过期时,如何使用java更新数据库行?

标签 java database jsp servlets

当用户 session 过期时,使用java如何更新数据库行? 这种情况包括 1.用户没有点击任何注销按钮 2.如果浏览器自动关闭。 3.系统直接断电情况。

那么,在这些情况下如何更新数据库行。现在我在注销点击条件时使用以下代码

Connection con = (Connection) new DB2Connection().getDatabaseConnection();
if(request.getParameter("flrmdn") != null)
{
String s1=request.getParameter("flrcaf");
String s2=request.getParameter("flrmdn");

String sql1="update table2 set FLRReference=0 where CAF='"+s1+"' and       MDN='"+s2+"' and FLRReference=1 ";
Statement st1=con.createStatement();
int vupdate=st1.executeUpdate(sql1);
}
else if(request.getParameter("indexmdn") != null)
{
String indexcaf=request.getParameter("indexcaf");
String indexmdn=request.getParameter("indexmdn");

String sql="update table set IndexReference=0 where CAF='"+indexcaf+"' and MDN='"+indexmdn+"' and IndexReference=1 ";
Statement st=con.createStatement();
int entryrows=st.executeUpdate(sql);
}


// Redirecting user to actual required page

if(request.getParameter("location").equalsIgnoreCase("login"))
{
response.sendRedirect("login.jsp?logout=true");
}

我使用过 httpsessionlistner()、sessioncreated()、sessiondistroyed(),但 sessioncreated 会自动调用,但 sessiondistroyed 不会在 session 过期时调用

最佳答案

使用 HttpSession.invalidate() 强制 session 失效您可以看到系统输出已打印,或者通过在 web.xml 中指定如下所示等待 session 自动过期。

<session-config>
      <session-timeout>5</session-timeout> 
</session-config>

关于java - 当用户 session 过期时,如何使用java更新数据库行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11863491/

相关文章:

java - Stax 解析两次

java - 使用 jdbc 将值插入数据库

java - 在jsp中调用java方法

javascript - 如何调用其他jsp页面脚本函数的javascript函数

java - 我应该使用什么 Eclipse API 来访问 jsp 文件的 dom 树?

java - 如何使用 Java 向 EMS Topic 发布消息

java - Apache Beam 中的嵌套前 N 个

SQL Server INSERT INTO 带 WHERE 子句

mysql - 通过比较列值来查询 3 个表,并且没有主键或外键关系

java - 如何使用java类在jsp页面中显示数据库中的值?