【发布时间】:2011-11-22 18:10:10
【问题描述】:
我刚刚将我的网络服务器从 Jetty 6.x 更新到 Jetty 8.0.1,出于某种原因,当我执行完全相同的请求时,有时响应已被 Gzip 压缩,有时则没有。
下面是 servlet 的 service() 方法开头的请求和响应的样子:
Request: [GET /test/hello_world?param=test]@11538114 org.eclipse.jetty.server.Request@b00ec2
Response: org.eclipse.jetty.servlets.GzipFilter$2@1220fd1
WORKED!
Request:[GET /test/hello_world?param=test]@19386718 org.eclipse.jetty.server.Request@127d15e
Response:HTTP/1.1 200
Connection: close
FAILED!
这是我的 GzipFilter 声明:
EnumSet<DispatcherType> all = EnumSet.of(DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.FORWARD,
DispatcherType.INCLUDE, DispatcherType.REQUEST);
FilterHolder gzipFilter = new FilterHolder(new GzipFilter());
gzipFilter.setInitParameter("mimeTypes", "text/javascript");
gzipFilter.setInitParameter("minGzipSize", "0");
context.addFilter(gzipFilter, "/test/*", all);
Javadoc 说:
GZIP Filter This filter will gzip the content of a response if:
The filter is mapped to a matching path ==>
The response status code is >=200 and <300
The content length is unknown or more than the minGzipSize initParameter or the minGzipSize is 0(default)
The content-type is in the comma separated list of mimeTypes set in the mimeTypes initParameter or if no mimeTypes are defined the content-type is not "application/gzip"
No content-encoding is specified by the resource
在我看来,所有这些条件都满足,除了最后一个“资源未指定内容编码”。如何验证?
另外,出于我也忽略的原因,当未使用 GzipFilter 过滤响应时,response.getWriter() 会引发 IO 异常。这是为什么呢?
【问题讨论】:
标签: java servlets jetty servlet-filters