Solved

Detecting and acting upon a JSP (http) session timeout

Posted on 2001-07-05
9
646 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
[X]
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
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

752 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