Solved

Detecting and acting upon a JSP (http) session timeout

Posted on 2001-07-05
9
634 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
Comment Utility
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
Comment Utility
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
Comment Utility
Any examples?
0
 
LVL 3

Expert Comment

by:sghosh092199
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 13

Author Comment

by:Philip Pinnell
Comment Utility
>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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now