Update on yesterday’s post. Here’s a sample listener that implements the HttpSessionListener interface:
****************************************************
package com.mindseye;
import javax.servlet.http.*;
import javax.servlet.http.HttpSessionListener;
/**
* @author Aaron Johnson
* ajohnson@mindseye.com
*
* CFMXHttpSessionListener: An object that implements the HttpSessionListener
* interface, allowing CFMX developers to use session_OnStart and session_OnEnd
* events. This class can only retrieve the J2EE session ID when “Use J2EE
* session variables” is checked in CFIDE –> Administrator –> Memory
* Variables.
*/
public class CFMXHttpSessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent se) {
// get the session just created
HttpSession newSession = se.getSession();
// do something with the new session (ie: log to db, write to a file, email to a friend
System.err.println(“A new session was created: session = ” + newSession.toString());
System.err.println(“Creation:” + newSession.getCreationTime());
}
public void sessionDestroyed(HttpSessionEvent se) {
// get the session just created
HttpSession newSession = se.getSession();
/* do stuff upon session end, (ie: delete items from this users shopping cart
if you’re persisting items to a db..)
*/
System.err.println(“A session was destroyed: session = ” + newSession.toString());
System.err.println(“Creation:” + newSession.getCreationTime());
}
}
****************************************************
And then to get this class to run, you must add
<listener>
<listener-class>
com.mindseye.CFMXHttpSessionListener
</listener-class>
</listener>
to CFMX\wwwroot\WEB-INF\web.xml and you must copy the compiled class to the CFMX\wwwroot\WEB-INF\classes\com\mindseye\ folder. Additionally, it appears that (and this makes sense) the session we’re getting information about is the J2EE session ID, not the CFID & CFTOKEN. This means that you must have “Use J2EE session variables” checked in CFIDE –> Administrator –> Memory Variables. Restart CFMX and then you can see the results in cfusionmx\runtime\logs\default-err.log
This class would probably be way more useful if it actually did something… like log the start and end times, the id and maybe the servlet context to a database, but each developer will probably want to do something different, so I’ll leave that unfinished. I’m *not* seeing the session_OnEnd fired in my testing… anyone wanna take a guess?
Here’s a sample application_OnStart for CFMX:
****************************************************
package com.mindseye;
import javax.servlet.*;
import javax.servlet.ServletContextListener;
/**
* @author Aaron Johnson
* ajohnson@mindseye.com
*
* CFMXServletContextListener: An object that implements the ServletContextListener
* interface, allowing CFMX developers to use application_OnStart and
* application_OnEnd events.
*/
public class CFMXServletContextListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
// do something on destroy()
System.err.println(“The application ‘” + sc.getServletContextName() + “‘ was destroyed”);
}
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
// do something on destroy()
System.err.println(“The application ‘” + sc.getServletContextName() + “‘ was started”);
}
}
****************************************************
Again, same thing, you must copy the compiled class files to cfusion\wwwroot\WEB-INF\classes\com\mindseye\, add a <listener> element to web.xml and restart CFMX. You’ll see the results in cfusion\runtime\logs\default-err.log.
I’d love to hear from Macromedia (or any other Java geeks) as to why I don’t get an results from sessionDestroyed().
Very helpful article
What does this do? Is this a jar file which is imported?
package com.mindseye;
Or a location of mindseye class file?
Dan
hey Dan,
You should read up on the Java Language Specification ‘package’ section:
http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#60384
‘package com.mindseye’ means that I want this class to be a member of the package com.mindseye, it may have made more sense to name the package ‘com.mindseye.listeners’ or some such.
Dear guy,
I want to know about Application -Specifcic variable,which are shared about all session,could you please send me a document or a sample code
Thank
Just wondering, is it possible to implement the same thing for the application scope?
I am really in need to call an action when the application scope times out..
Thanks for any help.
hi Taco,
Look at the application_OnEnd event (read down to the bottom part of the post) for the ability call an action when the application times out.
AJ