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

标签 java jsp jakarta-ee

我有一个使用 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/

相关文章:

java - 调用过滤器的顺序是什么?

java - 您将如何测试静态方法 URLEncoder.encode?

java - 从另一个类设置参数

java - 将 <%@include%> 与 apachetiles 这样的库一起使用是否正常?

java - java httpssession的生命周期

java - Hibernate:使用复杂对象的集合会引发异常

java - 如何通过ajax调用使用rest web服务

java - 如何在android studio中调用java项目的main方法

java - 为什么不能重写 jspService()?

java - 如何使用公钥的 mod 和 exp 初始化 Cipher 变量