Whatterz


Poor Man's HTTP Compression with ColdFusion

by Simon. Average Reading Time: about 2 minutes.

An interesting article I read in the CFDJ recently was entitled Poor Man’s HTTP Compression with ColdFusion. Almost every web application will benefit from the compression of content. A compression filter optimises the size of the content that is sent from a webserver to a web browser via the Internet. Since generating content and serving pages via the World Wide Web is the core behind web applications, it is simple components that aid these processes that are incredibly useful. This is where servlet filters come into play.

Servlet filters are tools available to web application developers. They are designed to be able to manipulate the request and responses that are sent to a web application, without manipulating the servlets, static pages like HTML and, in this case, CFM pages that are being used by the web application (unless of course that is the desired response). Servlet filters act like a chain of steps that a request and response must go through before reaching the page in the application.

Compressing Content Using a Servlet Filter

Compression is a process that reduces the number of bytes required to define a document in order to save disk space or transmission time. It is extremely useful for sending information across the web, because the speed at which people receive information from a web application is dependent upon how much data you are trying to send. The smaller the amount of information that is to be sent, the faster it can be sent. Therefore, compression and the associated responsiveness is a key component to retaining users and generating revenue from those retained users.

Compression can be effectively achieved by having a servlet filter conditionally pipe the produced content to a GZip-compressed file. GZip is supported by the HTTP protocol and almost all modern browsers (hence the servlet filter conditionally compresses the content).

GZip compression usually results in a 6:1 compression ratio, although this depends on how much content is being sent and what the content is.

Setting up the Servlet Filter in ColdFusion

Using the ColdBeans servlet filter found at the following URL:

http://www.servletsuite.com/servlets/gzipflt.htm

  • Download the GZipFilter.jar
  • Save GZipFilter.jar in the WEB-INF/lib folder in the ColdFusion Server wwwroot.
  • Edit the web.xml file in the WEB-INF folder in the ColdFusion Server wwwroot with the following code:
    <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>
  • Restart the ColdFusion Service

Now, when you invoke any .cfm page the GzipFilter will check out client’s browser settings. If the browser does not support gzip, the filter invokes resource normally. If the browser does support gzip, output will be compressed.

This article has been tagged

, , , , , , , , , , , , , , , , , , ,

Other articles I recommend

Eliminating Whitespace in ColdFusion

ColdFusion has never satisfactorily removed whitespace from generated content, however, removing this whitespace can dramatically improve your website’s performance. Take a 100KB page for example. If 20% of the page is made up of whitespace, that is 20% that is unneccessary and 20% bandwidth cost that can be saved.

How to Configure Apache to GZip Your Components

Compressing your Web components will help speed up your Website. The majority of your visitors will benefit as most all Web browsers support GZip compression. You’ll want to compress all text, which includes HTML, CSS, JavaScript, XML, JSON, etc.

Generic Syntax Highlighter (GeSHi) for ColdFusion

GeSHi started out as a module for the phpBB forum system to enable highlighting of programming languages, which was largely unavailable at the time. Since its conception it has spawned into a standalone project, supported by many web-based PHP content management systems.

  • Sami Hoda

    Glad you liked it!

  • your web.xml example looks wrong – shouldn’t “GzipFilter be between the “filter-name” tags, and *.cfm be between the “url-pattern” tags, instead of being after.

  • The CMS seems to mal-form the code, so I’ve added a screenshot.

  • jeff

    Nice article

    Will this servlet work on the 64 bit version of CF8? I guess I am asking whether servelets need to be compiled as 64 bit

  • Cheve

    I am trying to implement this filter in coldfusion8, I had it working in CF7, but when copying the jar file to LIB, and the above code to web.xml all I get is:
    HTTP/1.1 503 Server Error
    A server error occurred
    Server Error
    Either the Macromedia application server(s) are unreachable or none of them has a mapping to process this request.

    Any idea what am I doing wrong?
    I googled for an answer but there’s nothing about CF8 and this filter especifically so I asume it should work with no problems, but I cannot get it to work!

    Regards

  • Cheve

    Ok, so anyone having the same problem can fix it:

    My problem was that I was trying to configure the filter to work NOT with the normal CFM extension but with a new CFZ extension, so I can decide which files are compressed and which ones don’t, so if you want to implement this filter you have to add the CFZ extension to the IIS app extensions AND in web.xml in coldfusion you also have to add a new servlet-mapping:

    CfmServlet
    *.cfz

    Be sure to use the NEXT mapping number in the ID when adding the new one (id=”coldfusion_mapping_XX”)

    Regards

  • Cheve

    The code for the mapping got mangled when submiting, this is how it looks changing the brackets:

    
      CfmServlet
      *.cfz>/url-pattern>
    
    
    
    Regards

  • I have the same error … Either the Macromedia application server(s) are unreachable or none of them has a mapping to process this request.

    but have no idea what all the above information means… can someone tell me in layman’s terms how to rectify the problem ? Please email

  • Jim Cook

    Just found this, gave it a try on CF8.01/Win2003. All CFLOCATIONS failed to work, had to uninstall it. Bummer, 'cause it worked very well otherwise.

  • I4

    Did you find a work around for this? I seem to have the same issue.

  • Dat

    You should rewrite this filter to fix this bug