Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP Session Tracking

 
CS Gajanan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Anil Kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic