2Way HTTP Compression Servlet Filter
2Way compression filter is a Servlet filter that compresses and decompresses both HTTP requests and HTTP responses using gzip. It was originally written to compress SOAP messages, but it can be used for any Web application running in a J2EE Web container like Jetty, JRun or Tomcat.
HTTP compression can drastically reduce download time. Most Web servers and Web containers have built-in support for HTTP compression. If not, servlet filters can be used. Most of the solutions only compress the HTTP response which is fine for Web browsing where the request consists of just a few bytes. The situation is different if HTTP is used to transport SOAP messages because the average SOAP request message is the same size as the average SOAP response. To get the most out of HTTP compression, the request should also be compressed.
How it Works
Server and client negotiate about compression using HTTP header fields.
If a request is compressed a Content-Encoding header is added to the HTTP header fields. The value of this field indicates the compression algorithm. The filter decompresses a request if it finds a Content-Encoding header field.
GET /products/firefox/start/ HTTP/1.1 Host: www.mozilla.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Accept: text/xml,application/xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5
Keep-Alive: 300 Connection: keep-alive
The response is only compressed if the client is able to decompress it. The client tells the server about its ability to decompress in the response's Accept-Encoding header field. The client in the example is accepting compressed responses using gzip and deflate.
It is easy to use the filter in your own program without changing source code. To use the filter with a Web application, these steps have to be followed:
- Download the compression filter library 2wayfilter-1.2.jar and copy it into the Web application's WEB-INF/lib folder
- Configure the Web application to use the filter. Include the following filter and filter-mapping elements in the Web application's deployment descriptor. The deployment descriptor is in the file web.xml in the folder WEB-INF.
<web-app> ... <filter> <filter-name>2WayFilter</filter-name> <filter-class>com.osmoticweb.gzipfilter.GZIP2WayFilter</filter-class> </filter> <filter-mapping> <filter-name>2WayFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ... </web-app>
Use of this software is free of charge for both personal and commercial purposes.
predic8 HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS OR IMPLIED, RELATIVE TO THE SOFTWARE, INCLUDING BUT NOT LIMITED TO ANY WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR MERCHANTIBILITY. predic8 SHALL NOT BE LIABLE OR RESPONSIBLE FOR ANY DAMAGES, INJURIES OR LIABILITIES CAUSED DIRECTLY OR INDIRECTLY FROM THE USE OF THE SOFTWARE, INCLUDING BUT NOT LIMITED TO INCIDENTAL, CONSEQUENTIAL OR SPECIAL DAMAGES.