Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7529027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:32:49+00:00 2026-05-30T04:32:49+00:00

I have a web application built using jsp/servlets.The web application session is set to

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:32:50+00:00Added an answer on May 30, 2026 at 4:32 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I have a PHP Web application built using Zend Framework and MVC with
I have built a web application using Java EE platform that sells one of
I have a web application built in asp.net mvc using forms authentication. I am
I have a web application that needs to be built using PHP/MySQL. The application
We have a web application that has been built using MySQL / PHP /
I have tested a web application I have built using MVC 2 on local
I have a mobile web application built using the following versions :- JQuery Mobile
I have an ajax heavy web-application built using PHP, jQuery. I am looking for
I have a spring based web application MyWebapp built using maven and deployed on
I have a web application that needs to be built using a nant script.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.