Compressing Server Responses in Java Enterprise Applications
Compression ImplementationThis 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).
Figure 1. Clients Initiating Requests: The clients initiating requests need to understand that the message is compressed, and then properly decompress it.
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 FilterTo 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:
- Add the weblogicx-gzip.jar file from the code download into your WAR's WEB-INF/lib directory.
- 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>
- 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 FilterThis 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>
Page 2 of 3