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().