2009-04-01 10 views
10

का उपयोग करें हालांकि, मैं अपने प्रतिक्रियाओं को जीजेआईपी wheren के साथ संभवतः संपीड़ित करना चाहता हूं। मैंने Compression filter code का उपयोग हेडफास्ट साइट में मुफ्त डाउनलोड के लिए उपलब्ध कराने का प्रयास किया। यह एचटीएमएल, छवियों, सीएसएस और जावास्क्रिप्ट के लिए महान काम करता है।जीजेआईपी, जेएसओएन प्रतिक्रियाएं और JQuery

मैं अगला फ़िल्टर पोस्ट करता हूं। यह जांचता है कि क्या जीजेआईपी एक स्वीकार्य एन्कोडिंग है और यह सामग्री-एन्कोडिंग के रूप में gzip जोड़ता है। देखें: 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 वेब अनुप्रयोग में 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 ऑब्जेक्ट के अब और नहीं देख सकता। मैं संभाल के रूप में निम्नानुसार JSON अजाक्स कोड के टुकड़े के साथ JQuery के साथ कहता है:

$.post(url,parameters, function(json) { 
    // Do some DOM manipulation with the data contained in the JSON Object 
}, "json"); 

अगर मैं Firebug के साथ प्रतिक्रिया देखना यह खाली है।

क्या मुझे JSON प्रतिक्रियाओं में संपीड़न को छोड़ने के लिए अपने संपीड़न फ़िल्टर को अपवर्तक करना चाहिए? या इसके लिए कोई कामकाज है?

मेरे लिए, ऐसा लगता है कि JQuery JSON के रूप में प्रतिक्रिया को पहचान नहीं पा रहा है क्योंकि मैं Gzip संपीड़न जोड़ रहा हूं।

+0

यदि आप संपीड़न भाग के बिना अपना कोड चलाते हैं तो क्या यह सफलतापूर्वक प्रतिक्रिया पास करता है? –

+0

हां, जेएसओएन शामिल सब कुछ संपीड़न के बिना ठीक काम करता है –

+0

क्या आपको इसके लिए समाधान मिला? मेरे पास समान अनसुलझे मुद्दा है। यदि आप अपना उत्तर पोस्ट कर सकते हैं तो यह बहुत अच्छा होगा। – Lijo

उत्तर

3

यदि आप इसे संपीड़ित कर रहे हैं तो आपको एक और शीर्षलेख "सामग्री-एन्कोडिंग: gzip" जोड़ना होगा।

+0

मैंने पहले ही किया था। मैं अपनी पोस्ट संपादित करता हूं। वैसे भी जवाब के लिए धन्यवाद। –

0

क्या आपने यह सुनिश्चित करने के लिए एक स्पष्ट जावा-आधारित क्लाइंट के साथ प्रयास किया है कि यह jQuery या ब्राउज़र के साथ कोई समस्या है? यदि जावा क्लाइंट विफल रहता है, सर्वर प्रतिक्रिया के साथ कुछ गलत है।

लेकिन मुझे लगता है कि ब्राउज़र प्रत्यक्ष अनुरोधों के साथ असंपीड़न से निपट सकता है, यह शायद अजाक्स कॉल पर लागू नहीं होता है।

यह एक दिलचस्प सवाल है, मुझे आशा है कि हमें एक और निश्चित उत्तर मिलेगा। :)

5

यदि मुझे फ़ायरबग के साथ प्रतिक्रिया दिखाई देती है तो खाली है।

आपका सुराग है - यह एक JQuery समस्या नहीं है, यह सर्वर-साइड है। (मुझे डर है कि मैं आपकी मदद नहीं कर सकता, सुझाव देने के अलावा कि आप क्लाइंट-साइड को देखना बंद कर दें)

कोई समस्या नहीं है AJAX प्रतिक्रियाएं - अगर आप फायरबग में प्रतिक्रिया नहीं देख पा रहे हैं, तो JQuery इसे या तो नहीं देख सकता है।

+0

क्या यह सर्वर के बाद और ग्राहक से पहले हो सकता है? कुछ नेटवर्क तत्वों द्वारा क्रोधित एक समस्या? – Lijo

संबंधित मुद्दे