Ir al contenido principal

This is my blog, more about me at marianoguerra.github.io

🦋 @marianoguerra.org 🐘 @marianoguerra@hachyderm.io 🐦 @warianoguerra

Enable gzip compression in jetty webserver (static and dinamic content)

GZIP Compression

Guest post from Javier Dall' Amore

GZIP Compression can be used to reduce the amount of data being sent "over the wire". Compression is applied as a transport encoding. This can greatly improve webapplication performance, however it can also consume more CPU and some content (eg images) cannot be well compressed.

Static Content

The Jetty Default Servlet can serve precompressed static content as a transport encoding and avoid the expense of on-the-fly compression. This servlet, normally mapped to /, provides the handling for static content, OPTIONS and TRACE methods for the context. One of its init parameters is "gzip". If set to true, then static content will be served as gzip content encoded if a matching resource is found ending with ".gz". So if a request for "jquery.js" is received and the file "jquery.js.gz" exists, then it will be served as "jquery.js" with a gzip transport encoding.


<init-param>

<param-name>gzip</param-name>

<param-value>true</param-value>

</init-param>

GzipFilter(dynamic content)

The Jetty Gzip Filter is a compression filter that can be applied to almost any dynamic resource (servlet). It fixes many of the bugs in commonly available compression filters (eg handles all ways that content length may be set) and has been testing with Jetty continuations and suspending requests.

This filter will gzip or deflate the content of a response if:

  1. The filter is mapped to a matching path
  2. accept-encoding header is set to either gzip, deflate or a combination of those
  3. The response status code is >=200 and <300
  4. The content length is unknown or more than the minGzipSize initParameter or the minGzipSize is 0(default)
  5. The content-type is in the comma separated list of mimeTypes set in the mimeTypes initParameter or if no mimeTypes are defined the content-type is not "application/gzip"
  6. No content-encoding is specified by the resource

If both gzip and deflate are specified in the accept-encoding header, then gzip will be used.

Compressing the content can greatly improve the network bandwidth usage, but at a cost of memory and CPU cycles.

To enable gzip dynamic compression, add the this filter definition to your "web.xml". This works for jetty 7 but for jetty 6 you should replace the filter-class for org.mortbay.servlet.GzipFilter:


<filter>

<filter-name>GzipFilter</filter-name>

<filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>

<init-param>

<param-name>mimeTypes</param-name>

<param-value>text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,application/json,image/svg+xml</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>GzipFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>