gpt4 book ai didi

java - 更新 Java EE Web 应用程序中的注销数据

转载 作者:行者123 更新时间:2023-12-02 07:58:04 25 4
gpt4 key购买 nike

我有一个使用 jsp/servlet 构建的 Web 应用程序。Web 应用程序 session 设置为在 web.xml 中设置的用户不活动 5 分钟后超时。

现在我想在 session 超时发生之前更新一些与 session 超时的用户相关的详细信息。

假设用户已登录,并且由于用户选择在这种情况下保持不活动状态,我需要为 session 超时的用户更新一些信息。

我如何实现同样的目标。

package com.student.track;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class sessionInfo implements HttpSessionListener {


public void sessionCreated(HttpSessionEvent event) {

}
public void sessionDestroyed(HttpSessionEvent event) {

String query = "insert into SessionInfo values(?)";
try
{
runQuery(query);
}
catch(Exception e)
{
e.printStackTrace();
}

}

public static void runQuery(String query) throws Exception

{
Connection conn=null;
int result=0;
try
{
conn = getConnection();
PreparedStatement stat=null;
stat = conn.prepareStatement(query);
stat.setString(1,"first");
result = stat.executeUpdate(query);
}
catch(IOException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
conn.close();
}
catch (SQLException e)
{
throw e;
}
}

}

public static Connection getConnection() throws SQLException, IOException, Exception
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();

String drivers = props.getProperty("dbcb.jdbcDriver");
try
{
Class.forName(drivers);
}
catch(Exception e)
{
throw e;
}

if (drivers != null)
System.setProperty("jdbc.drivers", drivers);

String url = props.getProperty("dbcb.jdbcURL");
String username = props.getProperty("dbcb.dbUser");
String password = props.getProperty("dbcb.dbPassword");
return DriverManager.getConnection(url, username, password);
}


}

和 web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.student.track.InitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/InitServlet</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/orataglib</taglib-uri>
<taglib-location>tlds/orataglib_1_0_2.tld</taglib-location>
</taglib>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<resource-ref>
<description>Sybase Datasource example</description>
<res-ref-name>jdbc/mysybase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<listener>
<description>sessionListener</description>
<listener-class> com.student.track.sessionInfo </listener-class>
</listener>
</web-app>

最佳答案

相当简单,实现 HttpSessionListener 。容器将触发sessionDestroyed()如果它决定从缓存中删除 session 。

示例:

@WebListener /* or reister it in web.xml */
public final class MySessionListener implements HttpSessionListener {

@Override
public void sessionCreated(final HttpSessionEvent se) { }

@Override
public void sessionDestroyed(final HttpSessionEvent se) {

// obtain HTTP session
HttpSession session = se.getSession();

/* as you cannot retrieve the user ID from the
* HTTP session, getRemoteUser() is not available, you must
* read the user ID from a custom parameter
*/
String user = (String) session.getAttribute("myCustomAttribute");

// work with it

}

}

更新。要在 web.xml 中指定监听器,请将以下标记添加到 web.xml:

<listener>
<listener-class>com.test.MySessionListener</listener-class>
</listener>

关于java - 更新 Java EE Web 应用程序中的注销数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9358396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com