Configure GZIP Filter on Jetty 8 for dynamic compression of contents

Jetty provides Gzip Filter to enable dynamic compression of contents like html, javascript, css etc. Just a servlet filter 'org.eclipse.jetty.servlets.GzipFilter' needs to be configured in web.xml to save bandwidth and speed up your website.

Tools and Technologies used in this article

  1. Jetty 8
  2. Java Web Application

Note: Prior to Jetty 7, all versions were hosted in jetty.codehaus.org, so refer that site to get the correct package name of GzipFilter class. For example, In Jetty 6 we need to use org.mortbay.servlet.GzipFilter.

Update web.xml

Add GzipFilter in the 'web.xml' file of your web application to be deployed in Jetty server.

File: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="false">
    <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,image/svg+xml</param-value>
        </init-param>
    </filter>
 
    <filter-mapping>
        <filter-name>GzipFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

Use comma separated mimeTypes for which you want Jetty to apply GZIP compression.

Note: Don't use gzip compression for image (except SVG), video, PDF or other binary files. These are already compressed, so you will not get any additional benefit out of it. .

Download SrcCodes

All code samples shown in this post are available in the following link web.xml

References