aspose file tools*
The moose likes JSP and the fly likes JSP Session Tracking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP Session Tracking" Watch "JSP Session Tracking" New topic
Author

JSP Session Tracking

CS Gajanan
Greenhorn

Joined: Feb 08, 2004
Posts: 4
Hi all,
By default the session expires when it is idle more than the time specified in web.xml or by using maxinactiveinterval() method. I want to know how to find it out so that I can process it and perform some action. Can any one explain with program code.
Thanks in advance.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by CS Gajanan:
Hi all,
By default the session expires when it is idle more than the time specified in web.xml or by using maxinactiveinterval() method. I want to know how to find it out so that I can process it and perform some action. Can any one explain with program code.
Thanks in advance.

Welcome to JR CS !!
If you want to perform some action whenever the session is destroyed by the container, you can use the HttpSessionListener interface, declared in web.xml.
the methods in this interface are:
- sessionCreated(HttpSessionEvent event)
called when a session is created
- sessionDestroyed(HttpSessionEvent event)
called when a session is destroyed.
Notice that you can use the sessionDestroyed method to do any processing you require in case the session is destroyed. However, you cannot use any logic inside this method to retrieve values from the session itself. The session is invalidated and then this method is called.
hope it helps.
[ February 09, 2004: Message edited by: Andres Gonzalez ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Anil Kumar
Greenhorn

Joined: Jan 17, 2004
Posts: 11
Though it's always more fun to try out things but since the request was specifically for a code, here's it.
the class(bean) file should have something like
import java.util.*;
import javax.servlet.http.*;
public class sessionBeen implements HttpSessionBindingListener{
public sessionBeen(){
}
public void valueBound(HttpSessionBindingEvent evt){
Calendar cal = Calendar.getInstance();
System.out.println("Logged in at "+cal.getTime());
}
public void valueUnbound(HttpSessionBindingEvent evt){
Calendar cal = Calendar.getInstance();
System.out.println("Logged off at "+cal.getTime());
}
}
the jsp which can use this should have something like
<%@ page language="java" import="sessionBeen"%>
<html>
<body>
<%
sessionBeen oSessionBean = new sessionBeen();
session.setMaxInactiveInterval(60);
session.setAttribute("sessionObject",oSessionBean);
out.println("<A href='logoff.jsp'>Log Off</A>");
%>
</body>
</html>
the loggoff.jsp must have a session.invalidate()
This example lets you track when the user logged out or maximum inactive interval was reached.
A very basic working
HTH
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Welcome to the Ranch "AnilKumar"!
You'll find this forum a great place to seek help on JSP pages, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Thanks!
bear
JSP Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSP Session Tracking