【问题标题】:Updating Data on Logout in Java EE web application在 Java EE Web 应用程序中更新注销数据
【发布时间】:2012-02-20 08:59:32
【问题描述】:

我有一个使用 jsp/servlets 构建的 Web 应用程序。Web 应用程序会话在 web.xml 中设置的用户闲置 5 分钟后设置为超时。

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

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

我如何做到这一点。

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>

【问题讨论】:

    标签: java jsp jakarta-ee


    【解决方案1】:

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

    例子:

    @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>
    

    【讨论】:

    • 你能给个相同的样本吗?我在哪里做这些设置来达到同样的效果。
    • 我链接错了类,当然是HttpSessionListener。将添加一个示例
    • 感谢您的示例。我如何以及在何处添加此代码以便在注销时触发它。相同的程序是什么。
    • 只需在您的项目中创建一个类,与 servlet 相同。如果添加@WebListener 注解,容器应该会识别你的类并自动调用相应的方法。当然,这仅在您的注销功能确实破坏了会话时才有效。我会在方法中添加一些日志语句,这样您就可以看到它们何时真正被触发。
    • 现在我是否必须在 web.xml 中更新并编写此类代码。或者执行其中任何一个。
    猜你喜欢
    • 2012-07-22
    • 2012-06-09
    • 2012-11-25
    • 2016-02-22
    • 2012-04-27
    • 1970-01-01
    • 2016-10-05
    • 2011-04-12
    • 1970-01-01
    相关资源
    最近更新 更多