Configuring GzipFilter in Jetty

I am trying to configure Jetty to provide compressed html content. In web.xml, I install GzipFilter and map it to / *, but this does not seem to work. Here is the filter configuration:

<filter> <filter-name>GZipFilter</filter-name> <display-name>Jetty GZip Filter</display-name> <description>Filter that zips all the content on-the-fly</description> <filter-class>org.mortbay.servlet.GzipFilter</filter-class> <init-param> <param-name>mimeTypes</param-name> <param-value>text/html</param-value> </init-param> </filter> <filter-mapping> <filter-name>GZipFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

I'm just starting to use Jetty, so the solution can be ridiculously simple. If you can connect me with documentation that can help me, that's great too.

+3
source share
6 answers

Gzip compression

GZIP compression can be used to reduce the amount of data sent "over the cable." Compression is used as transport coding. This can significantly improve web application performance, but it can also consume more CPU, and some materials (like images) may not be well compressed.

Static content

By default, Jetty's Servlet can serve pre-compressed static content as transport encoding and avoid on-the-fly compression costs. If the initialization parameter "gzip" is set to true, Jetty will look for compressed static resources. Therefore, if a request is received for "foo.txt" and the file "foo.txt.gz" exists, it will be used as "foo.txt" with transport encoding gzip.

Gzipfilter

The Jetty Gzip filter is a compression filter that can be applied to almost any dynamic resource (servlet). It fixes many errors in public compression filters (for example, it processes all the ways in which the length of the content can be set) and was tested with the continuation of Jetty and the suspension of requests.

Some user agents may be excluded from compression to avoid some common browser errors (yes, that means IE!).

link to the doc pier: http://docs.codehaus.org/display/JETTY/GZIP+Compression

you can see the source code of Gzipfilter, here are a lot of useful notes: http://download.eclipse.org/jetty/stable-7/xref/org/eclipse/jetty/servlets/GzipFilter.html

+3
source

I will also answer this question, since I had a huge break trying to do this job, and I finally did it. In addition, I am not a major expert in the fine details of HTTP, so I will give an unprofessional answer.

Firstly, this is how I checked if my GZipFilter is working or not. Launched Firefox, made sure that I have a Firebug addon, launched the Firebug addon, went to the "Net" tab. Then I turned to the URL, which should return a GZipped response. Here is what Firebug shows:

enter image description here

The Size column indicates the size of the response. If you hover over the Size label with the mouse, it will tell you that if the response is compressed, then the compressed size of the response will be displayed.

All this was done with the Jetty GZip enabled. Then I removed the GZip filter declaration from my web.xml, restarted Jetty and retried the test. This time around the answer was the same size as before, which clearly indicates that GZip compression is not working.

After some trial and error, I did this in Firebug under the “Request Headers” section to see the value for the “Accept” header. I noticed that there were such values ​​as "application / xml" and "text / xml", but the way I set my original GZIp filter parameter "mimeTypes" contained only "text / xml" (and was missing) the application / XML "). It was configured like this:

 <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,application/json,application/xml; charset=UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>GzipFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

After adding the value "application / xml" to the list:

 <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,application/xml,text/css,application/javascript,image/svg+xml,application/json,application/xml; charset=UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>GzipFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

I reinstalled my previous test, and now I'm pretty sure that the response size is smaller:

enter image description here

Also note that now the response headers contain additional fields called "Content-Encoding" with a value of "gzip".

So, basically the idea is to check what values ​​you send in the "Accept" request header, and make sure that all these values ​​are configured in the initialization parameter of the "mimeTypes" GZip file.

+3
source

Sometimes using Gzipfilter has some problems, depending on how you handle buffers and flushing. Thus, using org.eclipse.jetty.servlets.IncludableGzipFilter (which actually extends GzipFilter) can solve your problems.

+1
source

At marina 9.3:

  • edit the jetty.conf file and include the xml file "jetty-gzip.xml"

  • modify start.ini and add "--module = servlets"

  • edit the jetty-gzip.xml file and configure the mime types you need.

  • Restart the marina and check again.

+1
source

What mistake? Are you getting problems with the class or something else? If the path to the classes, you must make sure that the gzipfilter class is available for the runtime of the berth or that it will die.

0
source

Are you sending a request with the request header "Content-Encoding: gzip"?

-1
source

Source: https://habr.com/ru/post/913156/


All Articles