What is Gzip Handler?
The Jetty GzipHandler is a compression handler that you can apply to any dynamic resource (servlet). Gzip fixes many of the bugs in commonly available compression filters: it works with asynchronous servlets and handles all ways to set content length. It has been tested with Jetty continuations and suspending requests.
Compressing the content can greatly improve the network bandwidth usage, but at the cost of memory and CPU cycles. The DefaultServlet is capable of serving pre-compressed static content, which saves memory and CPU. By default, the GzipHandler will check to see if pre-compressed content exists, and pass the request through to be handled by the DefaultServlet.
Gzip Rules:
GzipHandler will gzip the content of a response if:
It is mapped to a matching path
The request method is configured to support gzip
The request is not from an excluded User-Agent
accept-encoding header is set to gzip
The response status code is >=200 and <300 p="">300>
The content length is unknown or more than the minGzipSize initParameter or the minGzipSize is 0(default)
The content-type does not match an excluded mime-type
No content-encoding is specified by the resource
Implementation:
1. Remove Gzip Filter from application web.xml file
First of all, comment any Gzip filter tag given in the deployed application web.xml file as Gzip filter is depreciated in Jetty Server v9.3:
Location:
<jetyy-server>\webapps\ROOT\WEB-INF\web.xml
<!-- <filter> <filter-name>GzipFilter</filter-name> <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>GzipFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> -->
2. Add Gzip Handler parameter in jetty server
Set Gzip Handler parameter true in servlet tag of Jetty Server’s webdefault.xml:
Location:
Set Gzip Handler parameter true in servlet tag of Jetty Server’s webdefault.xml:
Location:
<jetty-server>\etc\webdefault.xml
<init-param> <param-name>gzip</param-name> <param-value>true</param-value> </init-param>
3. Modify configuration of Jetty Server Gzip Handler
We can modify configuration of Gzip Handler through jetty-gzip.xml file available at jetty server as shown below:
Location:
<jetty-server>\etc\jetty-gzip.xml
<Configure id="Server" class="org.eclipse.jetty.server.Server"> <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="excludedAgentPatterns"> <Array type="String"> <Item><Property name="jetty.gzip.excludedUserAgent" deprecated="gzip.excludedUserAgent" default=".*MSIE.6\.0.*"/></Item> </Array> </Set> <Set name="includedMethods"> <Array type="String"> <Item>GET</Item> <Item>POST</Item> </Array> </Set> <Set name="includedPaths"> <Array type="String"> <Item>/*</Item> </Array> </Set> </New> </Arg> </Call> </Configure>
Where minGzipSize denotes
Content will only be compressed if content length is either unknown or greater than minGzipSize.
checkGzExists
True by default. If set to false, the handler will not check for pre-compressed content.
compressionLevel
The compression level used for deflate compression. (0-9).
includedMethods
List of HTTP methods to compress. If not set, only GET requests are compressed.
excludedAgentPatterns
A list of regex patterns for User-Agent names from which requests should not be compressed.
includedPaths
List of paths to consider for compression.
4. Activation of Gzip Module
In Jetty Server 9.3, Gzip module is given in the name of gzip.mod(jetty-server-location\modules\gzip.mod) which we need to activate first before Jetty server start. And for activation there is a command given --add-to-start through which you can activate any module of jetty server.
First open command prompt from jetty server installed location and hit below command:
Syntax: java -jar start.jar --add-to-start=gzip
Now we have activated the GzipHandler to configuration of the server successfully.
After that you can start jetty server as per your normal stat up process.
5. Listing Active Modules of Jetty Server
If you want to check which module is running or activated at jetty server, --list-modules command would be given at jetty server startup time.
Syntax: java -jar start.jar --list-modules
Above listing shows activated modules on Jetty Server 9.3 and this will give a brief about every activated module as well.
6. Checking Gzip Handler at Network Layer
You can check gzip activation at Network tab of browser by just Click on User small request rows button like as shown below:
Now you can check that size of response data reduced from 67.9 MB to 1.4 MB as expected which due to gzip handler activation.
When you click on any request like connect above and select Headers Tab,
Content-Encoding is also activated with gzip.
Reference: http://www.eclipse.org/jetty/documentation/9.3.x/gzip-filter.html
No comments:
Post a Comment