October 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Compressing Server Responses in Java Enterprise Applications

  • August 5, 2009
  • By Vlad Kofman
  • Send Email »
  • More Articles »

Compression Implementation

This article covers two similar methods to achieve compression in JEE applications:
  • A WebLogic compression filter
  • A PlanetJ compression filter

Both methods are indifferent to the contents of the response they are compressing and act on the response as a whole. In other words, these methods act as filters and can be used to compress XML responses, string responses, and even binary responses (see Figure 1).

Both methods are configured in a similar way; both use the gzip algorithm to compress data. Note that because the compression algorithm is done entirely in Java it could be slower then a native implementation. (See the code download for the actual JAR files.)

WebLogic Compression Filter

To set up this compression filter in your web application, import the JAR library file into the project, add filter mapping in the web.xml, and then map all resources that can benefit from compression, such as .txt, .log, .html, or /wsdl/* for web services. Take these simple steps as follows:
  1. Add the weblogicx-gzip.jar file from the code download into your WAR's WEB-INF/lib directory.
  2. Register the gzip filter in your web.xml as shown in the WEB-INF/web.xml:
    <filter>
      <filter-name>GZIPFilter</filter-name>
      <filter-class>weblogicx.servlet.gzip.filter.GZIPFilter</filter-class>
    </filter>
  3. Map the filter like this (An example is also shown in the web.xml included with the code download):
    <filter-mapping>
       <filter-name>GZIPFilter</filter-name>
       <url-pattern>/*.html</url-pattern>
       <url-pattern>/service/wsdl/*</url-pattern>
    </filter-mapping>

PlanetJ Compression Filter

This compression filter is set up in almost an identical way as the WebLogic compression filter. You need to include the JAR and update the web.xml. Here is an example of the web.xml settings for the PlanetJ filter:
<filter>
      <filter-name>CompressingFilter</filter-name>
      <filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class>
      <init-param>
         <param-name>debug</param-name>
         <param-value>true</param-value>
      </init-param>
      <init-param>
         <param-name>jakartaCommonsLogger</param-name>
         <param-value>com.planetj.servlet.filter.compression.CompressingFilter</param-value>
      </init-param>
</filter>
<filter-mapping>
      <filter-name>CompressingFilter</filter-name>
      <url-pattern>/service/wsdl/*</url-pattern>
</filter-mapping>

Tags: Java EE



Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel