File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpSessionListener: What were they thinking?

 
Vaishali Joshi
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has anybody observed this:
sessionCreated() is called right after the session is created. At this time there is nothing in the session except the sessionid. So at most, we can log a message saying " session"+sessionid+" created.
sessionDestroyed() is called after the session is invalidated. This means you cannot retrieve anything from the session. Not even the id. So, how am I supposed to use it?
Has anybody used this interface to do anything meaningful??
I'm lost!
 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishali,
sessionDestroyed() is called before invalidating the session.
wrox says you can save the data to database or any other cleanups before the session is being invalidated in sessionDestroyed() method.
Well in the same way you can do any session initializations required for your session in sessionCreated() method.
good luck and happy new year,
Ramdhan Yadav K
 
Vaishali Joshi
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramdhan Kotamaraja:

sessionDestroyed() is called before invalidating the session.


Well, you should refer to the API.


wrox says you can save the data to database or any other cleanups before the session is being invalidated in sessionDestroyed() method.

I would wish so. But wrox is wrong. You cannot do this. I have tried this on Tomcat40 as well.
 
jyothi ve
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try wrox example with HttpSessionListener.
Session Destroyed method getting called when Session timed out. Just now I tried this, its working for me with Tomcat 4.0. Even after closing the browser, you have to wait until timeout period to be over to see the Session Destroyed method get called.
Set the session timeout to 1 min in web.xml to see the results faster

[This message has been edited by jyothi ve (edited December 31, 2001).]
 
Vaishali Joshi
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your sessionDestroyed() method try retrieving any attribute from the session. I get an IllegalStateException. This the means the method is called AFTER the session is destroyed. This is what I am talking about.
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>This the means the method is called AFTER >>the session is destroyed. This is what I >>am talking about.
Exactly ture. However the purpose of these methods are not to access any part of the session, because you will get an IllegalStateException. The purpose is to close resources like connections, which are usually created in the sessionCreated() method.
Kind of like the finally in a try/catch/finally or better yet as in finalize method of Object.
In reading the API, it says it is a way to "notify" that the session is to be created, or has just been destroyed. I can see a lot of use for this. Some of the use is on the server, which can pool Servlets, and also Sessions.
Hope that clears some things up, and that I didn't get something wrong there.
Mark
 
Vaishali Joshi
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot see why would you create a database connection in say MyHttpSessionListener class. Even if you do, how will you use that connection? You won't even have a reference to the object of this class.
The only handles you have are the sessionCreated() or sessionDestroyed() methods. For all you know, these method may not even be called if nobody accesses the web application !!!

Some of the use is on the server, which can pool Servlets, and also Sessions.

Could you please be more clear about this? Any example would be helpful.
 
jyothi ve
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope the use of HttpSessionListener class is to initilize or track information before serving the requests.
For example: For Admin purpose to findout number of active sessions in the given point of time, how many users accessed the site etc.
when session destroy called number of active users will get reduced.
If you want to initialize some session specific information you can do in this method
[This message has been edited by jyothi ve (edited December 31, 2001).]
 
Vaishali Joshi
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jyothi ve:
I hope the use of HttpSessionListener class is to initilize or track information before serving the requests.
For example: For Admin purpose to findout number of active sessions in the given point of time, how many users accessed the site etc.
when session destroy called number of active users will get reduced.
If you want to initialize some session specific information you can do in this method

That's what I am trying to say. It seems to be the purpose of this interface but you cannot do any of things that you have mentioned. Can you please write some code to show how will you do this?
All you can do is add some generic attribute (I mean, not even user specific because at this time, you won't know whose session is this) in the sessions as soon as they are created using the sessionCreated() method. Yes, you may get value for this attribute from the database.
But I am unable to think of any other use of this interface. Specifically, I more frustrated by the presence of the "good for nothing" sessionDestroyed() method.
 
jyothi ve
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vaishali Joshi,
Below code tells the uses what I mentioned previously i.e tracking active sessions, number of visitors for this site
********* Java Class implenets Listener *****
package test;
import java.util.Date;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class CounterListenerExample implements HttpSessionListener {
private static int counter = 0;
private static int activeCount = 0;

public void sessionCreated(HttpSessionEvent evt) {
long time = evt.getSession().getCreationTime();
System.out.println("A new session was crated at " + new Date(time));
counter++;
activeCount++;
}
public void sessionDestroyed(HttpSessionEvent evt) {
activeCount--;
System.out.println("A session was destroyed at ");
}
public static String getCounterInfo() {
return "Number of active sessions on server = " + activeCount + "\n" + "No of total visitors to date = " + counter;
}
}
*********************************************
************ JSP which uses above class *****
<%@ page import="test.CounterListenerExample" %>
<html>
<head><title> Example session counter </title></head>
<body bgcolor="#ffffcc">
<h3 align="center"> Counter Example : <h3>
<br>
<%= CounterListenerExample.getCounterInfo() %>
</body>
</html>
*********************************************
Let me know whether I am able to fulfill what I mentioned previously or not
[This message has been edited by jyothi ve (edited December 31, 2001).]
 
Vaishali Joshi
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jyothi! I get it now.
 
James Thomas
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am considering great possibilities with this interface. Specific values added to the session can trigger a tracking process that amounts to an automatic asynchronous notification. Has anyone done this?
 
Anoop Keni
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I used the similar code and mapped it in web.xml in the listener tag. When a new call is made to the jsp page the session is created and incremented in the method sessionCreated(HttpSessionEvent se). but when i close the browser the destroy method is not called.

Any idea how the listener can call sessionDestroyed(HttpSessionEvent se) method? Here i am decrementing the count.

When i retrieve the the count value ... it is only incremented value.

Heres the code for AppSessionCouner

--------------------------------------------------------------------
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;


public class AppSessionCouner implements HttpSessionListener
{

private static int activeSessions = 0;

// Create a new session.
public void sessionCreated(HttpSessionEvent se){
System.out.println("Inside sessionCreated");
System.out.println("Before New session createb = " + activeSessions);
activeSessions++;
System.out.println("After New session count = " + activeSessions);
}

// Destroy the session
public void sessionDestroyed(HttpSessionEvent se){
System.out.println("Inside sessionDestroyed");
if (activeSessions > 0)
{
System.out.println("Count before destroyed = " + activeSessions);
activeSessions--;
System.out.println("Count after destroyed = " + activeSessions);
}

}

// get the count of active session.
public static int getActiveSessions(){
return activeSessions;
}

}

------------------------------------------------------------------

Heres the code i added into web.xml

<!-- Listeners -->
<listener>
<listener-class>
com.camo.po.servlets.AppSessionCouner
</listener-class>
</listener>

-------------------------------------------------------------------


Heres the code I used to check the count on the jsp


<%=AppSessionCouner.getActiveSessions()%>


Thanx.
Hoping for a quick reply.
 
Phani Kumar Sripada
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anoop,

Try setting the session time out period in the DD.

Add the below lines to your web.xml. It makes the session to get destroyed one minute after it is created.

<session-config>
<session-timeout>1</session-timeout>
</session-config>

Hope this helps!

Regards,
Phani.

[ July 30, 2004: Message edited by: Phani Kumar Sripada ]
[ July 30, 2004: Message edited by: Phani Kumar Sripada ]
 
Lakshmi Anantharaman
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Along these lines . How to capture the browser window closing with Java ?

Resposes appreciated
[ March 29, 2005: Message edited by: Laksh Anan ]
 
ritesh hk
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
firstly, I think the session listener will be more useful for initializing and destroying session attributes such as DB Connections. Session Attributes can be any Object. so you can create DBConnection, and store it as a session attrbute when the session is created. and close these connecteion when the session is destoryed!


and for the Window closing.. u gotta use javascript. window.close() or something like that. you could make this javascript method call a servelt which could do some processing for you (such as: send the user an email that he/she should perform a graceful logout by hitting the logout button )


hth

-ritesh
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic