在Jetty中设置GzipFilter的问题 [英] Problem setting up GzipFilter in Jetty

查看:364
本文介绍了在Jetty中设置GzipFilter的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置Jetty来提供压缩的html内容。在web.xml中,我设置GzipFilter并将其映射到/ *,但这似乎不起作用。以下是过滤器配置:

 < filter> 
< filter-name> GZipFilter< / filter-name>
<显示名称> Jetty的GZip过滤器< /显示名称>
< description>将所有内容即时压缩的过滤器< / description>
< filter-class> org.mortbay.servlet.GzipFilter< / filter-class>
< init-param>
< param-name> mimeTypes< / param-name>
< param-value> text / html< / param-value>
< / init-param>
< / filter>

< filter-mapping>
< filter-name> GZipFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>

我刚刚开始使用Jetty,因此解决方案可能会非常简单。如果您可以将我链接到可能对我有帮助的文档,那也太好了。 我也要回答这个问题,因为我已经做了大量的工作,我终于做到了。另外,我并不是HTTP细节方面的专家,所以我会给出一个非专业的答案。



首先,我是如何检查我的GZipFilter工作与否。开始Firefox,确保我有Firebug的插件,启动Firebug的插件,去网络选项卡。然后我访问了应该返回一个GZIPE响应的URL。这里是萤火虫显示:



大小列显示响应的大小。如果将鼠标悬停在大小列标签上,它会告诉您,如果响应被压缩,则会显示响应的压缩大小。



这一切都是在Jetty中启用的GZip过滤器完成的。然后我从我的web.xml中删除GZip过滤器声明,重新启动Jetty并重复测试。这一次的回应和以前一样大小,这清楚地表明,GZip压缩不起作用。

经过多次试验和错误,我所做的是看在请求标题部分的Firebug中查看Accept标题的值。我注意到,这里有application / xml和text / xml的值,但是我配置我的GZIp过滤器的初始参数mimeTypes的方式只包含text / xml(并且丢失了application / 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;字符集= UTF-8< / PARAM值>
< / init-param>
< / filter>
< filter-mapping>
< filter-name> GzipFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>

将application / xml值添加到列表中后,如下所示:

 < filter> 
< filter-name> GzipFilter< / filter-name>
< filter-class> org.eclipse.jetty.servlets.GzipFilter< / filter-class>
< init-param>
< param-name> mimeTypes< / param-name>
text / html,text / plain,text / xml,application / xhtml + xml,application / xml,text / css,application / javascript,image / svg + xml,application / json,应用/ XML;字符集= UTF-8< / PARAM值>
< / init-param>
< / filter>
< filter-mapping>
< filter-name> GzipFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>

我重复了之前的测试,现在确实已经足够了。 / p>



另外请注意,现在,报告的响应头包含一个名为Content-Encoding的额外字段,其值为gzip。

所以基本上这个想法是检查你的请求接受头中发送什么样的值,并确保所有这些值都在GZip过滤器的mimeTypes初始参数中配置。


I'm trying to setup Jetty to serve compressed html content. In web.xml I setup GzipFilter and mapped it to /* but this doesn't seem to work. Here's the filter configuration:

<filter>
 <filter-name>GZipFilter</filter-name>
 <display-name>Jetty's GZip Filter</display-name>
 <description>Filter that zips all the content on-the-fly</description>
 <filter-class>org.mortbay.servlet.GzipFilter</filter-class>
 <init-param>
  <param-name>mimeTypes</param-name>
  <param-value>text/html</param-value>
 </init-param>
</filter>

<filter-mapping>
 <filter-name>GZipFilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

I'm just starting to use Jetty, so the solution might be ridiculously simple. If you can link me to documentation that might help me, that would be great too.

解决方案

I'm gonna answer this too, since I've had a huge headake trying to make this work, and I finally did it. Also, I'm not a major expert in the fine details of HTTP so I'll give a non-professional answer.

First, here's how I checked if my GZipFilter was working or not. Started Firefox, made sure I had the Firebug addon, started the Firebug addon, went to the "Net" tab. Then I accessed the URL which should return a GZipped response. Here's what Firebug shows:

The "Size" column shows the size of the response. If you hover over the "Size" column label with your mouse, it will tell you that if the response is compressed, then it will display the compressed size of the response.

This all was done with the GZip filter enabled in Jetty. I then removed the GZip filter declaration from my web.xml, restarted Jetty and repeated the test. This time around the response had the exact same size as before, which clearly indicated that the GZip compression was not working.

After multiple trial and errors, what I did is look in Firebug at the "Request Headers" section to see the value for the "Accept" header. I've noticed that here this had values such as "application/xml" and "text/xml", but the way I had configured my GZIp filter's init param "mimeTypes" only contained "text/xml" (and was missing "application/xml"). It was configured like so:

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

After adding the "application/xml" value to the list like so:

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

I redid my previous test, and sure enough now the reported size of the response was much smaller:

Also notice that now, the reported Response Headers contain an extra field called "Content-Encoding" with a value of "gzip".

So basically the idea is to check what kind of values you send in your Request "Accept" header and make sure that all those values are configured in the GZip filter's "mimeTypes" init param.

这篇关于在Jetty中设置GzipFilter的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆