Leon Atherton Leon is a developer at IDRsolutions and product manager for BuildVu. He oversees the BuildVu product strategy and roadmap in addition to spending lots of time writing code.

How to enable GZIP Compression in Tomcat, Jetty, GlassFish and Payara

1 min read

GZIP Compression is a widely supported method of reducing the file size of the content hosted by your Java Application Server. This reduces bandwidth usage and improves load times. We recommend enabling GZIP compression for .html, .css, .js, .svg, and .json files.

Follow the instructions below to enable GZIP compression of static assets hosted by Tomcat, Jetty, GlassFish or Payara.

Tomcat

  1. Open conf/server.xml
  2. Find <service name="Catalina">
  3. Find the <Connector /> tag inside it
  4. Add the following attributes to the Connector tag:
    • compression="on"
    • compressionMinSize="2048"
    • noCompressionUserAgents=""
    • compressableMimeType="text/html,text/css,text/plain,text/javascript,application/javascript,application/json,image/svg+xml"
  5. Restart the server and you’re done

Jetty

  1. Open etc/jetty.xml
  2. Find the <Configure id="Server" class="org.eclipse.jetty.server.Server"> tag
  3. Add in the following lines inside the Configure tag
    <Call name="insertHandler">  
        <Arg>  
            <New id="GzipHandler" class="org.eclipse.jetty.server.handler.gzip.GzipHandler">  
                <Set name="minGzipSize"><Property name="jetty.gzip.minGzipSize" deprecated="gzip.minGzipSize" default="2048"/></Set>  
                <Set name="checkGzExists"><Property name="jetty.gzip.checkGzExists" deprecated="gzip.checkGzExists" default="false"/></Set>  
                <Set name="compressionLevel"><Property name="jetty.gzip.compressionLevel" deprecated="gzip.compressionLevel" default="-1"/></Set>  
                <Set name="inflateBufferSize"><Property name="jetty.gzip.inflateBufferSize" default="0"/></Set>  
                <Set name="syncFlush"><Property name="jetty.gzip.syncFlush" default="false" /></Set>  
     
                <Set name="excludedAgentPatterns">  
                    <Array type="String">  
                        <Item><Property name="jetty.gzip.excludedUserAgent" deprecated="gzip.excludedUserAgent" default=".*MSIE.6\.0.*"/></Item>  
                    </Array>  
                </Set>  
     
                <Set name="includedMethodList"><Property name="jetty.gzip.includedMethodList" default="GET" /></Set>  
                <Set name="excludedMethodList"><Property name="jetty.gzip.excludedMethodList" default="" /></Set>  
            </New>  
        </Arg>  
    </Call>
  4. Restart the server and you’re done

GlassFish

  1. Open the GlassFish admin console (localhost:4848 for example)
  2. Go to the HTTP settings via: Configurations → server-config → Network Config → Network Listeners → http-listener-1/http-listener-2 (change the settings in both of these) → HTTP (tab at the top of the page)
  3. Edit the ‘Compression’ setting to be ‘on’
  4. Change the ‘Compressible Mime Types’ to use text/html,text/css,text/plain,text/javascript,application/javascript,application/json,image/svg+xml
  5. Click save and you’re done

Payara

  1. Open the Payara admin console (localhost:4848 for example)
  2. Go to the HTTP settings via: Configurations → server-config → Network Config → Network Listeners → http-listener-1/http-listener-2 (change the settings in both of these) → HTTP (tab at the top of the page)
  3. Edit the ‘Compression’ setting to be ‘on’
  4. Change the ‘Compressible Mime Types’ to use text/html,text/css,text/plain,text/javascript,application/javascript,application/json,image/svg+xml
  5. Click save and you’re done


Do you need to solve any of these problems?

Display PDF documents in a Web app
Use PDF Forms in a web browser
Convert PDF Documents to an image
Work with PDF Documents in Java

Are you a Developer working with PDF files?

Learn more about PDF file format
Leon Atherton Leon is a developer at IDRsolutions and product manager for BuildVu. He oversees the BuildVu product strategy and roadmap in addition to spending lots of time writing code.

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2022. All rights reserved.