{"id":237,"date":"2003-01-27T12:30:21","date_gmt":"2003-01-27T16:30:21","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=237"},"modified":"2003-01-27T12:30:21","modified_gmt":"2003-01-27T16:30:21","slug":"more-on-coldfusion-httpsessionlistener-interface","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/01\/27\/more-on-coldfusion-httpsessionlistener-interface\/","title":{"rendered":"More on ColdFusion &amp; HttpSessionListener interface"},"content":{"rendered":"<p>Update on yesterday&#8217;s post.  Here&#8217;s a sample listener that implements the HttpSessionListener interface:<\/p>\n<p>****************************************************<br \/>\npackage com.mindseye;<\/p>\n<p>import javax.servlet.http.*;<br \/>\nimport javax.servlet.http.HttpSessionListener;<\/p>\n<p>\/**<br \/>\n * @author Aaron Johnson<br \/>\n * ajohnson@mindseye.com<br \/>\n *<br \/>\n * CFMXHttpSessionListener: An object that implements the HttpSessionListener<br \/>\n * interface, allowing CFMX developers to use session_OnStart and session_OnEnd<br \/>\n * events. This class can only retrieve the J2EE session ID when &#8220;Use J2EE<br \/>\n * session variables&#8221; is checked in CFIDE &#8211;&gt; Administrator &#8211;&gt; Memory<br \/>\n * Variables.<br \/>\n *\/<\/p>\n<p>public class CFMXHttpSessionListener implements HttpSessionListener {<\/p>\n<p>\tpublic void sessionCreated(HttpSessionEvent se) {<\/p>\n<p>\t\t\/\/ get the session just created<br \/>\n\t\tHttpSession newSession = se.getSession();<\/p>\n<p>\t\t\/\/ do something with the new session (ie: log to db, write to a file, email to a friend<br \/>\n\t\tSystem.err.println(&#8220;A new session was created: session = &#8221; + newSession.toString());<br \/>\n\t\tSystem.err.println(&#8220;Creation:&#8221; + newSession.getCreationTime());<\/p>\n<p>\t}<\/p>\n<p>\tpublic void sessionDestroyed(HttpSessionEvent se)  {<\/p>\n<p>\t\t\/\/ get the session just created<br \/>\n\t\tHttpSession newSession = se.getSession();<\/p>\n<p>\t\t\/* do stuff upon session end, (ie: delete items from this users shopping cart<br \/>\n\t\t if you&#8217;re persisting items to a db..)<br \/>\n\t\t*\/<br \/>\n\t\tSystem.err.println(&#8220;A session was destroyed: session = &#8221; + newSession.toString());<br \/>\n\t\tSystem.err.println(&#8220;Creation:&#8221; + newSession.getCreationTime());<\/p>\n<p>\t}<br \/>\n}<\/p>\n<p>****************************************************<br \/>\nAnd then to get this class to run, you must add <\/p>\n<p>&lt;listener&gt;<br \/>\n&nbsp;&nbsp;&lt;listener-class&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;com.mindseye.CFMXHttpSessionListener<br \/>\n&nbsp;&nbsp;&lt;\/listener-class&gt;<br \/>\n&lt;\/listener&gt;<\/p>\n<p>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&#8217;re getting information about is the J2EE session ID, not the CFID &amp; CFTOKEN.  This means that you must have &#8220;Use J2EE session variables&#8221; checked in CFIDE &#8211;&gt; Administrator &#8211;&gt; Memory Variables.  Restart CFMX and then you can see the results in cfusionmx\\runtime\\logs\\default-err.log<\/p>\n<p>This class would probably be way more useful if it actually did something&#8230; 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&#8217;ll leave that unfinished.  I&#8217;m *not* seeing the session_OnEnd fired in my testing&#8230; anyone wanna take a guess?<\/p>\n<p>Here&#8217;s a sample application_OnStart for CFMX:<\/p>\n<p>****************************************************<br \/>\npackage com.mindseye;<\/p>\n<p>import javax.servlet.*;<br \/>\nimport javax.servlet.ServletContextListener;<\/p>\n<p>\/**<br \/>\n * @author Aaron Johnson<br \/>\n * ajohnson@mindseye.com<br \/>\n *<br \/>\n * CFMXServletContextListener: An object that implements the ServletContextListener<br \/>\n * interface, allowing CFMX developers to use application_OnStart and<br \/>\n * application_OnEnd events.<br \/>\n *\/<br \/>\npublic class CFMXServletContextListener implements ServletContextListener {<\/p>\n<p>\tpublic void contextDestroyed(ServletContextEvent sce) {<\/p>\n<p>\t\tServletContext sc = sce.getServletContext();<\/p>\n<p>\t\t\/\/ do something on destroy()<br \/>\n\t\tSystem.err.println(&#8220;The application &#8216;&#8221; + sc.getServletContextName() + &#8220;&#8216; was destroyed&#8221;);<\/p>\n<p>\t}<\/p>\n<p>\tpublic void contextInitialized(ServletContextEvent sce) {<\/p>\n<p>\t\tServletContext sc = sce.getServletContext();<\/p>\n<p>\t\t\/\/ do something on destroy()<br \/>\n\t\tSystem.err.println(&#8220;The application &#8216;&#8221; + sc.getServletContextName() + &#8220;&#8216; was started&#8221;);<br \/>\n\t}<br \/>\n}<br \/>\n****************************************************<\/p>\n<p>Again, same thing, you must copy the compiled class files to cfusion\\wwwroot\\WEB-INF\\classes\\com\\mindseye\\, add a &lt;listener&gt; element to web.xml and restart CFMX. You&#8217;ll see the results in cfusion\\runtime\\logs\\default-err.log.<\/p>\n<p>I&#8217;d love to hear from Macromedia (or any other Java geeks) as to why I don&#8217;t get an results from sessionDestroyed().<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update on yesterday&#8217;s post. Here&#8217;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 &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/01\/27\/more-on-coldfusion-httpsessionlistener-interface\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">More on ColdFusion &amp; HttpSessionListener interface<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/237"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=237"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/237\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}