2014-09-23 107 views
4

我試圖在Jetty 9上啓用gzip壓縮。我不想在我的web.xml中配置它,所以基於Jetty文檔我已經配置了一個override-web。 XML。我沒有在嵌入模式下使用Jetty,而是作爲容器。如何爲Jetty 9啓用GZIP

在我的{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> 

碼頭似乎加載這個很好,但gzip壓縮不適用於任何響應。我說Jetty加載了這個罰款,因爲之前當我試圖將override-web.xml放入webapps文件夾時,Jetty正在抱怨。

我已經通過了這裏的各種問題,但沒有一個人似乎有這個答案。 任何幫助表示讚賞。

+0

你想在碼頭一個Servlet 2.5體驗9? (只是問,因爲你的override-web.xml聲明瞭2.5,而且我們不知道你的cc.war聲明爲什麼) – 2014-09-23 19:05:52

+0

我的cc.war中的web.xml聲明瞭version = 2.5。 – IceMan 2014-09-23 20:17:58

回答

2

我認爲你應該可以在webdefault.xml中配置通用過濾器。嘗試在那裏註冊Gzip過濾器。

+0

只有在上下文xml deployable中引用webdefault.xml時纔會使用它。它在所有部署故意不使用默認情況下。請參閱[test-jetty-webapp示例](https://github.com/eclipse/jetty.project/blob/jetty-9.2.3.v20140905/tests/test-webapps/test-jetty-webapp/src/main/ config/demo-base/webapps/test.xml#L29) – 2014-09-23 22:00:20

+0

你是對的。但是在不觸及戰爭的情況下放置過濾器仍然是一個方便的地方,對吧? – kaqqao 2014-09-23 22:04:00

+0

如果您將webdefault.xml複製到您自己的副本並對其進行修改,那麼修改原生webdefault.xml並不是很有前途,並且不會使用$ {jetty.base} vs $ { jetty.home}功能非常好。 – 2014-09-23 22:15:59

2

一個保持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> 
+0

你也可以用'jetty-env.xml'做一個類似的事情(雖然它可能不是架構理想的地方):'(對於Jetty 8.1;包名可能已在Jetty 9中更改) – 2017-10-14 16:40:53