但是,我想尽可能用GZIP压缩我的回复。我尝试在headfirst网站上免费下载Compression filter code。它适用于HTML,图像,CSS和JavaScript。
我接下来发布了过滤器。它检查GZIP是否为可接受的编码,并将gzip添加为Content-Encoding。请参阅:wrappedResp.setHeader("Content-Encoding", "gzip");
public class CompressionFilter implements Filter {
private ServletContext ctx;
private FilterConfig cfg;
/**
* The init method saves the config object and a quick reference to the
* servlet context object (for logging purposes).
*/
public void init(FilterConfig cfg)
throws ServletException {
this.cfg = cfg;
ctx = cfg.getServletContext();
//ctx.log(cfg.getFilterName() + " initialized.");
}
/**
* The heart of this filter wraps the response object with a Decorator
* that wraps the output stream with a compression I/O stream.
* Compression of the output stream is only performed if and only if
* the client includes an Accept-Encoding header (specifically, for gzip).
*/
public void doFilter(ServletRequest req,
ServletResponse resp,
FilterChain fc)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// Dose the client accept GZIP compression?
String valid_encodings = request.getHeader("Accept-Encoding");
if ( (valid_encodings != null) && (valid_encodings.indexOf("gzip") > -1) ) {
// Then wrap the response object with a compression wrapper
// We'll look at this class in a minute.
CompressionResponseWrapper wrappedResp = new CompressionResponseWrapper(response);
// Declare that the response content is being GZIP encoded.
wrappedResp.setHeader("Content-Encoding", "gzip");
// Chain to the next component (thus processing the request)
fc.doFilter(request, wrappedResp);
// A GZIP compression stream must be "finished" which also
// flushes the GZIP stream buffer which sends all of its
// data to the original response stream.
GZIPOutputStream gzos = wrappedResp.getGZIPOutputStream();
gzos.finish();
// The container handles the rest of the work.
//ctx.log(cfg.getFilterName() + ": finished the request.");
} else {
fc.doFilter(request, response);
//ctx.log(cfg.getFilterName() + ": no encoding performed.");
}
}
public void destroy() {
// nulling out my instance variables
cfg = null;
ctx = null;
}
}
我正在使用下一个代码在Struts Web应用程序中发送JSON响应。
public ActionForward get(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
JSONObject json = // Do some logic here
RequestUtils.populateWithJSON(response, json);
return null;
}
public static void populateWithJSON(HttpServletResponse response,JSONObject json) {
if(json!=null) {
response.setContentType("text/x-json;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
try {
response.getWriter().write(json.toString());
} catch (IOException e) {
throw new ApplicationException("IOException in populateWithJSON", e);
}
}
}
没有压缩它工作正常但如果我压缩JSON响应,我再也看不到我的JSON对象了。我使用JQuery处理带有代码片段的JSON Ajax调用,如下所示:
$.post(url,parameters, function(json) {
// Do some DOM manipulation with the data contained in the JSON Object
}, "json");
如果我看到Firebug的响应,它就是空的。
我应该折射我的压缩过滤器以跳过JSON响应中的压缩吗?或者有解决方法吗?
对我来说,看起来JQuery不能将响应识别为JSON,因为我正在添加Gzip压缩。
答案 0 :(得分:5)
如果我看到Firebug的响应呢 是空的。
有你的线索 - 它不是JQuery问题,它是服务器端。 (我担心我无法帮助你,除了建议你不要再看客户端了)
gzipping ajax响应没有问题 - 如果你在Firebug中看不到响应,那么JQuery也看不到它。
答案 1 :(得分:3)
如果要压缩它,则必须再添加一个标题“content-encoding:gzip”。
答案 2 :(得分:0)
您是否尝试使用基于Java的显式客户端来确保jQuery或浏览器出现问题?如果java客户端出现故障,服务器响应就会出错。
但我猜测,虽然浏览器可以处理直接请求的解压缩,但这可能不适用于Ajax调用。
这是一个有趣的问题,我希望我们能得到一个更确定的答案。 :)