Solved

Detecting and acting upon a JSP (http) session timeout

Posted on 2001-07-05
9
643 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

763 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