Tuy nhiên, tôi muốn nén phản hồi của mình bằng GZIP nếu có thể. Tôi đã thử sử dụng Compression filter code có sẵn để tải xuống miễn phí tại trang web headfirst. Nó hoạt động tốt cho html, hình ảnh, css và javascript.Sử dụng GZIP, phản hồi JSON và JQuery
Tôi đăng bộ lọc tiếp theo. Nó kiểm tra nếu GZIP là một mã hóa được chấp nhận và nó thêm gzip là Content-Encoding. Xem: 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;
}
}
Tôi đang sử dụng mã tiếp theo để gửi phản hồi JSON trong ứng dụng web Struts.
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);
}
}
}
Nó hoạt động tốt mà không cần nén nhưng nếu tôi nén phản hồi JSON, tôi không thể thấy các đối tượng JSON của mình nữa. Tôi xử lý các cuộc gọi JSON Ajax với JQuery với đoạn mã như sau:
$.post(url,parameters, function(json) {
// Do some DOM manipulation with the data contained in the JSON Object
}, "json");
Nếu tôi thấy phản hồi với Firebug thì trống.
Tôi có nên khúc xạ bộ lọc nén của mình để bỏ qua nén trong phản hồi JSON không? hoặc có một giải pháp cho điều này?
Đối với tôi, có vẻ như JQuery không nhận ra phản hồi là JSON vì tôi đang thêm nén Gzip.
Nếu bạn chạy mã của mình mà không có phần nén, thành công có vượt qua được phản hồi không? –
Có, mọi thứ hoạt động liên quan đến JSON hoạt động tốt mà không cần nén –
Bạn đã nhận được giải pháp cho điều này chưa? Tôi có vấn đề tương tự chưa được giải quyết. Nó sẽ là tuyệt vời nếu bạn có thể gửi câu trả lời của bạn. – Lijo