Category Archives: ColdFusion

More on ColdFusion & HttpSessionListener interface

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

ColdFusion & HttpSessionListener interface

Couple weeks ago some guys from Macromedia stopped by to yack with us about what we thought could be improved and/or added to the next version of CFMX. One of the things that came up a couple times was that it would be nice to have session OnStart and OnEnd functionality as well as application OnStart and OnEnd. Reading this month’s Java Developer’s Journal, it has an article on the HttpSession object (I’d link to it, but they charge for their content… too bad, I bet they’d be getting alot more traffic and readers if they’d provide their content for free), which provides multiple listener objects, HttpSessionListener, HttpSessionBindingListener, HttpSessionAttributeListener, and HttpSessionActivationListener. I don’t have CFMX here at home, but it seems like it would be possible to write a class that implements those either of those interfaces (probably the sessionCreated() and sessionDestroyed methods of the HttpSessionListener object), register that class in the web.xml of WEB-INF for your application like this:

<listener>
  <listener-class>
    com.yourApp.yourSessionListener
  </listener-class>
</listener>

Turns out you could do the same type of thing w/ javax.servlet.ServletContextListener [contextDestroyed() and contextInitialized()] and then register:

<listener>
  <listener-class>
    com.yourApp.yourApplicationListener
  </listener-class>
</listener>

in your web.xml and then… hypothetically, you’d have session OnStart, session OnEnd, application OnStart and application OnEnd just like ASP.

I’ll try it out tomorrow, unless you beat me to it.

Macromedia: ColdFusion MX: ColdFusion Server (on multihomed servers) displays wrong page

Macromedia: ColdFusion MX: ColdFusion Server (on multihomed servers) displays wrong page: “If you have Macromedia ColdFusion MX Server installed on multihomed web servers (web servers with two or more virtual webroots), and if multiple files have the same name in each virtual webroot (for instance, there is an index.cfm in several of the virtual webroots), the first file loaded will display for all virtual webroots instead of the correct file for each virtual webroot.”

Verity Spider tips & tricks

Thanks to Phil for sending me a link to the Verity Spider tips & tricks on daemon.com.au. Daemon is/was a big Spectra shop and probably used the spider to search Spectra sites on a regular basis. So why doesn’t that page show up in a google search for “verity spider” or “verity spider tips“? Maybe it’s because of the way their content management system works, where each page is denoted by a CF UUID appended to the URL. This method probably helps the developers, but in the long run, isn’t so good for getting ranked or even indexed by the larger search engines… which led me to todays’ research: mod_rewrite. I got my MCSE from Microsoft back a couple years ago, so my first exposure to web servers was IIS. IIS was then and for the most part, is now very pointy clicky (although I’ve heard that .NET IIS will have a text-based configuration file). Anyway, Apache wasn’t something I played with much until the last year, when I brought up a couple linux machines and thus Apache. So today I dove headfirst into mod_rewrite and came up a solution for making the next version (due out anyday now) of karensrecipes.com more search engine friendly. In short, to get to a recipe on the development site right now, you’d type in something like this:

http://www.karensrecipes.com/recipes/detail.jsp?r=18

Again, just like the link I mentioned above, this is not an example of how to impress the search engines. Some kung foo regular expressions and a dab of JKMount knowledge and we now get something like this:

http://www.karensrecipes.com/recipes/18/Steamed_Mussels.jsp

and in your Apache httpd.conf:

RewriteEngine on
RewriteRule ^/recipes/([0-9]+)/.*$ /recipes/detail.jsp?r=$1 [PT]

which in English says something like “if the request starts with ‘/recipe/’ and then is followed by any number of digits and then is followed by a ‘/’ and any number of other characters, then rewrite the URL to this… (wanna know more about regular expressions? get this fabulous book!)

Pretty snazzy eh? It gives me warm feelings inside because my JSP/Servlet code doesn’t have any knowledge that funny stuff is being done to the URL in Apache, which means you can do all sorts of chicanery to your URL without having to change a lick of server side code.

ColdFusion CFC’s

Working with ColdFusion CFC’s today, ran into a couple problems and I’m wondering if I’m alone or just inexperienced (or both):

a) when invoking a CFC as a webservice, do you have to have a CFIDE virtual directory in IIS in order for the CFC to function? (ie: I’m using multiple virtual hosts in IIS, if I don’t have a CFIDE virtual directory defined in IIS, calls to my webservice located in folder of the virtual host aren’t picked up)

b) can you use custom tags via cfmodule in a CFC? I’m getting an error (specifically “coldfusion.tagext.lang.IncludeTag$NoSuchIncludeTemplateException : Could not find the included template..” ) when using CFMODULE to call a custom tag. I’m absolutely positive that the custom tag path is correct… exact same page in the same directory (just not a CFC) uses it fine… Seems almost like CFC’s lose track of where they live. I saw a thread on forums, but no answer other than check your mapping… which I’ve done.