Solved

Detecting and acting upon a JSP (http) session timeout

Posted on 2001-07-05
9
640 Views
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?

TIA
0
Comment
Question by:Philip Pinnell
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 2

Expert Comment

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

Or,
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.
0
 
LVL 2

Expert Comment

by:bagi112599
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
doc.
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 6258733
Any examples?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:sghosh092199
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!



}
0
 
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.
0
 
LVL 3

Expert Comment

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

Yes.

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.
0
 
LVL 2

Accepted Solution

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

<%  
     if(mySessionCleanerBean==null){
          // no MySessionCleanerBean set yet
          HttpSession session = request.getSession(true);
            session.setAttribute("sessionlistener",
                        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());
   }
}
0
 
LVL 3

Expert Comment

by:modder
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
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20146121 "

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:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=NAME_OF_TOPIC_AREA_HERE&qid=XXXXXXXXXXX (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

modder
Community Support
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 6417456
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

829 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