Wednesday, September 04, 2002

Fun with Filters and Listeners

Among the cooler new things in CFMX, which becomes available to us by way of the underlying J2EE engine, may be the concept of filters and listeners.

In the JSP/servlet world, these are discussed as a means to provide some sort of global functionality to your applications. Now we can use them with ColdFusion as well.

This first discussion will focus on filters. These could allow every request to your application (or a part of the application) to cause something to be done before running the requested page. In a way, this may seem much like CF's application.cfm.

The filter might make a decision about what template to execute as a result of analyzing the characteristics of the request (the ip address or user agent of the requester), or it might even modify the incoming request data.

And just as we have onrequestend.cfm which can cause some code to execute before a page is returned to the user, filters in jsp/servlets can also be used to do post-processing for them. It might modify the output before sending to the requester, or it might block it entirely.

Now with CFMX, we can use filters with CF templates.

Examples of how you might use them include authentication, logging and auditing, image conversion, data compression, localization,
XSLT transformations of XML content, encryption, caching and more.

You can learn more about filters at There are also articles on listeners O'Reilly and Java Boutique, as well as a chapter in More Servlets and JSP by Marty Hall.

While the focus of most discussions in JSP/servlet articles and books is on how to create such filters, the fact is that you may find one created by someone and available on a public repository of them that already does something interesting for you.

And we as CF developers don't really need to know any Java (or even understand servlets or JSP) to be able to leverage a filter that someone else has written.

There are repositories of filters at various places. One of them is the ColdJava suite. This really has nothing to do with ColdFusion, despite the name.

In a later article, I'll walk through the process of using one of these, but with this pointer to where to learn how to work with them, and where to find them, many will have enough to figure things out on their own.

Before concluding, I had mentioned that another new JSP/servlet concept available to us was "listeners". There are two kinds of listeners, application and session, which can act as triggers that can cause execution of a program when an event takes place in the application or session contexts.

Now, the application and session contexts, as well as the programs that can be fired in response to events, would all be Java ones. What does that mean to us? Well, again, Java code may already exist. Further, we can setup the CFMX environment to use listeners for the application and session contexts just as with JSPs or servlets.

In a later blog entry, I'll show some practical applications of using filters and listeners in CFMX.

For now, you can learn more about listeners and the "servlet life cycle" on your own at O'Reilly and Sun.

No comments: