我也要回答這個問題,因爲我已經做了大量的工作,我終於做到了。另外,我並不是HTTP細節方面的專家,所以我會給出非專業的答案。
首先,我是如何檢查我的GZipFilter是否工作。開始使用Firefox,確保我擁有Firebug插件,啓動Firebug插件,進入「網絡」選項卡。然後我訪問了應該返回GZip的響應的URL。下面是螢火蟲顯示:
「尺寸」列顯示響應的大小。如果將鼠標懸停在「大小」列標籤上,它會告訴您,如果響應被壓縮,則它會顯示響應的壓縮大小。
這一切都是在Jetty中啓用的GZip過濾器完成的。然後我從我的web.xml中刪除了GZip過濾器聲明,重新啓動了Jetty並重復了測試。這次響應的尺寸與之前的尺寸完全相同,這清楚地表明GZip壓縮不起作用。
經過多次試驗和錯誤,我所做的是在Firebug的「請求標題」部分查看「Accept」標題的值。我注意到,這裏有「application/xml」和「text/xml」等值,但是我配置GZIp過濾器的初始參數「mimeTypes」的方式只包含「text/xml」(並且缺少了「應用程序/ XML「)。它配置,像這樣:
<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>
加入了「應用程序/ XML」值列表,像這樣經過:
<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>
我重做我以前的測試,果然,現在的報告的大小反應要小得多:
另請注意,現在報道的響應頭包含稱爲「內容編碼」的額外字段的值「壓縮」。
所以基本上這個想法是檢查你的請求「接受」頭髮送什麼樣的值,並確保所有這些值都配置在GZip過濾器的「mimeTypes」初始參數。
我終於有時間回到這個問題。我沒有收到任何錯誤。但是我沒有看到實際發生的任何gzip。我如何檢查過濾器是否實際運行? – Misha 2010-09-27 17:30:16
@Misha,你可以使用TCP數據包跟蹤軟件,或使用一些在線工具檢查,如http://www.gidnetwork.com/tools/gzip-test.php – spektom 2011-06-19 13:21:40