简体   繁体   中英

Updating Data on Logout in Java EE web application

I have a web application built using jsp/servlets.The web application session is set to timeout after say 5 mins of inactivity by the user which is set in web.xml.

Now i want to update some details just before this session timeout occurs pertaining to a user whose session is getting timed out.

Say a user has logged in and incouse of time the user chooses to stay inactive in this situation i need to update some information for the user whose session is being timed out.

How do i achieve the same.

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);
    }


    }

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

Fairly easy, implement a HttpSessionListener . The container will trigger sessionDestroyed() if it decides to remove a session from its cache.

Example:

@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

    }

}

Update. To specify the listener in web.xml add the following tags to web.xml:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM