How to enable gzip for Jetty 9

I am trying to enable gzip compression on Jetty 9. I do not want to configure it in my web.xml, so based on the Jetty documentation I have configured override-web.xml. I do not use Jetty in native mode, but as a container.

In my {jetty.home}/webapps , I have a war file - cc.war. I also defined cc.xml as follows

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <!-- ================================================================== Configure and deploy the test web application in $(jetty.home)/webapps/test Note. If this file did not exist or used a context path other that /test then the default configuration of jetty.xml would discover the test webapplication with a WebAppDeployer. By specifying a context in this directory, additional configuration may be specified and hot deployments detected. ===================================================================== --> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <!-- Required minimal context configuration : --> <!-- + contextPath --> <!-- + war OR resourceBase --> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <Set name="contextPath">/</Set> <Set name="war"><Property name="jetty.webapps"/>/cc.war</Set> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <!-- Optional context configuration --> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <!-- <Set name="extractWAR">true</Set> <Set name="copyWebDir">false</Set> --> <!--<Set name="defaultsDescriptor"><Property name="jetty.home" default="."/>/etc/webdefault.xml</Set>--> <Set name="overrideDescriptor"><Property name="jetty.webapps" default="."/>/cc.d/override-web.xml</Set> </Configure> 

In the cc.d folder, I have override-web.xml as follows:

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <filter> <filter-name>GzipFilter</filter-name> <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class> <init-param> <param-name>methods</param-name> <param-value>GET,POST</param-value> </init-param> <init-param> <param-name>mimeTypes</param-name> <param-value>text/html,text/xml,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json,application/xml,application/xml+xhtml,image/svg+xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>GzipFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 

Jetty seems to load this penalty, but gzip compression does not apply to any of the answers. I say that Jetty downloaded this penalty because earlier, when I tried to place override-web.xml in the webapps folder, Jetty complained.

I covered various questions here on SO, but none of them seemed to answer that. Any help is appreciated.

+5
source share
2 answers

I think you should be able to configure common filters in webdefault.xml . Try registering the Gzip filter there.

+2
source

One way to save the GZIP configuration from your application while running in a Jetty container (not built-in) is to add a filter to the XML context:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath"><!-- set context path --></Set> <Set name="war"><!-- set war path --></Set> <Call name="addFilter"> <Arg>org.eclipse.jetty.servlets.GzipFilter</Arg> <Arg>/*</Arg> <Arg> <Call name="of" class="java.util.EnumSet"> <Arg><Get name="REQUEST" class="javax.servlet.DispatcherType" /></Arg> </Call> </Arg> <Call name="setInitParameter"> <Arg>mimetypes</Arg> <Arg>text/html,text/xml,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json,application/xml,application/xml+xhtml,image/svg+xml</Arg> </Call> <Call name="setInitParameter"> <Arg>methods</Arg> <Arg>GET,POST</Arg> </Call> </Call> </Configure> 
+2
source

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


All Articles