如何为Jetty 9启用GZIP

6
我正在尝试在Jetty 9上启用gzip压缩。我不想在我的web.xml中进行配置,因此根据Jetty文档,我已经配置了override-web.xml。我没有使用嵌入式模式,而是作为一个容器来使用Jetty。
在我的{jetty.home}/webapps文件夹中,我有一个war文件-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文件夹中,我有一个名为override-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会抱怨。

我已经浏览了这里的各种问题,但似乎没有一个答案适合我的情况。 非常感谢您的帮助。


你是否想在Jetty 9上获得Servlet 2.5的体验?(只是问一下,因为你的override-web.xml声明了2.5,而我们不知道你的cc.war声明为什么) - Joakim Erdfelt
我的cc.war中的web.xml声明了version=2.5。 - IceMan
2个回答

2

我认为你应该能够在 webdefault.xml 中配置常见过滤器。尝试在那里注册 Gzip 过滤器。


只有在上下文XML可部署文件中引用webdefault.xml时,它才会被使用。这是有意的,并不会默认用于所有部署。请参见test-jetty-webapp示例 - Joakim Erdfelt
没错。但这仍然是一个方便的地方,可以在不触及代码的情况下放置过滤器,对吧? - kaqqao
最好将webdefault.xml复制到您自己的副本中并进行修改,直接修改原文件不太具有未来性,并且不能很好地利用${jetty.base}与${jetty.home}功能。 - Joakim Erdfelt
@kaqqao 现在它可以工作了!!我之前尝试在webdefault.xml中设置它,但是我没有在我的cc.xml中引用它。我第一次尝试使用Jetty来替换当前环境中的tomcat,并且需要将其部署到Jetty容器中。我不知道默认情况下不使用webdefault.xml。我将来会考虑嵌入式部署。 - IceMan
@JoakimErdfelt 有什么想法为什么 override-web.xml 没有起作用?根据 Jetty 文档,建议使用 override-web.xml。 - IceMan
显示剩余3条评论

2

在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>

你也可以使用 jetty-env.xml 来实现类似的功能(虽然这可能不是架构上理想的位置):<Set name="handler"><New class="org.eclipse.jetty.server.handler.GzipHandler"/></Set>(适用于 Jetty 8.1;在 Jetty 9 中包名称可能已更改)。 - Janaka Bandara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接