[英]How to enable GZIP for Jetty 9
我試圖在Jetty 9上啟用gzip壓縮。我不想在我的web.xml中對其進行配置,因此根據Jetty文檔,我已經配置了override-web.xml。 我不是在嵌入式模式下使用Jetty,而是將其用作容器。
在我的{jetty.home}/webapps
文件夾中,我有戰爭文件-cc.war。 我還定義了一個cc.xml如下
<?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>
在文件夾cc.d中,我按如下所示重寫了web.xml:
<?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似乎可以很好地加載,但是gzip壓縮未應用於任何響應。 我說Jetty加載得很好,因為之前我嘗試將override-web.xml
放在webapps文件夾中時,Jetty抱怨。
我已經在這里討論了SO的各種問題,但是似乎沒有一個答案。 任何幫助表示贊賞。
我認為您應該能夠在webdefault.xml中配置常見過濾器。 嘗試在此處注冊Gzip過濾器。
在Jetty容器(非嵌入式)中運行時,將GZIP配置排除在應用程序之外的一種方法是將過濾器添加到上下文XML:
<?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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.