Greg's Blog

helping me remember what I figure out

CFMX and Filters

| Comments

I am back on the performance trail. For a while I have been meaning to use gZip to compress files sent by the server and for the most part I was focusing on letting the web server handle this, but I have just come across an article that let’s CFMX do the job: enter servlet filters.

The article in question was published in the CFDJ magazine back in February 2003. Here’s what you need to know about Filters:

  • A filter is a Java program
  • Can be executed before or after CFMX template (sounds like Application.cfm and OnRequestEnd.cfm?? The main difference being that you can control the execution of Filters [conditional processing, apply to all templates not just .cfm ones, etc…])
  • Can manipulate HTTP headers/requests, branch your code and modify the HTTP responses
  • YOu can write your own or use existing ones.

And here are some of the advantages as highlighted in the article of the control you can exercise over the execution of Filters:

  • A single template (of any type, not just .cfm)
  • All templates in a given directory and subdirectories
  • All templates of a given file type
  • All templates on the entire server

And here are a few resources where you can find some that have already been created:

The latter is where we found the GZip filter (Grab it here)and in the following we’ll discuss how to implement it. You’ll need to copy the JAR file into your cfusionmx\wwwroot\WEB-INF\lib folder (where cfusionmx is your servers install directory). Or if you are running CFMX as an instance on JRun, then you’ll need to copy it into: JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\lib.

Next you’ll need to configure your CFMX server to use that Filter, locate your web.xml file [usually found in: \JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF] and open it for editing [just in case anything goes wrong, remember to make a back up before editing this file]. Scroll to the end of the file and before the closing </web-app> tag copy the following lines in:

<filter>
<filter-name>GZIPFilter</filter-name>
<filter-class>com.cj.gzipflt.GzipFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GZIPFilter</filter-name>
<url-pattern>*.cfm</url-pattern>
</filter-mapping>

The final step requires you to restart the CFMX server so that the changes can take effect. Now if you are using the JRun web admin interface when carrying out the restart you may come across a few error messages, such as it took too long for the server to re-start. If you get this message and click on the home link in the top left hand corner of the interface, it will refresh the page and you should see that your server is running. If not remove the lines we added to web.xml, re-start your server and start again!

Here are links to resources relating to this article: