Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Detecting and acting upon a JSP (http) session timeout

Posted on 2001-07-05
Medium Priority
Last Modified: 2013-11-23
I have a web based application using JSPs and Java.

When a user's http session times out I want to detect this and run some logout code.

How do I do this?

Question by:Philip Pinnell
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2

Expert Comment

ID: 6255501
Set a database with the clients/sessions and a thread to verify from time to time the users state and act accordingly.

Set a java server to listen to a port and handle the sessions by recording them in a vector or something and have them checked from time to time. Then connect to it within the jsp.

Expert Comment

ID: 6256495
You can  use a class that implements
HTTPSessionBindingListener interface. When
session is ending, it will be notified.
Have a look at the HTTPSessionBindingListener API
LVL 13

Author Comment

by:Philip Pinnell
ID: 6258733
Any examples?
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!


Expert Comment

ID: 6261659
You need to implement the valueBound and valueUnBound methods in HTTPSessionBindingListener Interface.

import javax.servlet.http.HttpSessionBindingListener;

public class MyServlet extends ... implements HttpSessionBindingListener

public valueBound(HttpSessionBindingEvent he)
// You are connected to a Session


public valueUnbound(HttpSessionBindingEvent he)
// You are being disconnected from a session


Hope that helps!

LVL 13

Author Comment

by:Philip Pinnell
ID: 6264122
>public class MyServlet extends ... implements HttpSessionBindingListener

This seems to imply you are talking about servlets. I am using JSPs and beans. Would the same apply? Could one of my beans implement the HttpSessionBindingListener interface in the same way?

This may be academic as I have found my own method of doing what I want to do. I have added the necessary cleanup code to finalize method of a bean that has session scope. There may be an issue with the timing of when the finalize method get run but I am not sure yet.

Expert Comment

ID: 6264878
> Could one of my beans implement the HttpSessionBindingListener interface in the same way?


finalize() method is called once by the garbage collector on an object when garbage collection determines that there are no more references to the object. You can put debug statements in the finalize() to see when the finalize() method is called and it is in sync with the session timeout. It will not be prudent to put a code in finalize()that clears up system wide shared resources.

Accepted Solution

bagi112599 earned 268 total points
ID: 6265325
In your JSP:
<jsp:useBean id="mySessionCleanerBean" class="MySessionCleanerBean" scope="session"/>

          // no MySessionCleanerBean set yet
          HttpSession session = request.getSession(true);
                        new MySessionCleanerBean());

where MySessionCleanerBean is:

class MySessionCleanerBean implements HttpSessionBindingListener {
 public MySessionCleanerBean() {

 public void valueBound(HttpSessionBindingEvent event) {
   System.err.println("MySessionCleanerBean bounded for session " + event.getSession().getId());

 public void valueUnbound(HttpSessionBindingEvent event) {
   //do your clean up
   System.err.println("MySessionCleanerBean done for session " + event.getSession().getId());

Expert Comment

ID: 6417341
Hello andycrofts,

I have the points on this question to 67. Simply award the question to one of the experts that you want to give points.

The remainder of the 200 points you can use to post dummy questions. Just title them
"Points for <expert ID>".

In the question body, just state:
"For your help with "

Once you've created the dummy question, you should find out what it's URL is. To do that, click on the "Other Questions" link at the top of this page, which brings you to the main page for this topic area. Your new dummy question will be listed at the top. Then, click on the dummy question to find out its URL, which will look a bit like this: (a number)

Finally, come back to this question and post a comment saying "Posted dummy question for <Expert ID>", you can find it here: <url of dummy question>

Thanks for your cooperation

Community Support

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question