Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1036289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:40:45+00:00 2026-05-16T14:40:45+00:00

How to gzip an ajax response with Struts2? I tried to create a filter

  • 0

How to gzip an ajax response with Struts2? I tried to create a filter but it didn’t work. At client-side I’m using jQuery and the ajax response I’m expecting is in json.

This is the code I used on server:

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gz = new GZIPOutputStream(out);
    gz.write(json.getBytes());
    gz.close();

I’m redirecting the response to dummy jsp page defined at struts.xml.

The reason why I want to gzip the data back is because there’s a situation where I must send a relatively big sized json back to the client.

Any reference provided will be appreciated.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:40:46+00:00Added an answer on May 16, 2026 at 2:40 pm

    You shouldn’t randomly gzip responses. You can only gzip the response when the client has notified the server that it accepts (understands) gzipped responses. You can do that by determining if the Accept-Encoding request header contains gzip. If it is there, then you can safely wrap the OutputStream of the response in a GZIPOutputStream. You only need to add the Content-Encoding header beforehand with a value of gzip to inform the client what encoding the content is been sent in, so that the client knows that it needs to ungzip it.

    In a nutshell:

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    OutputStream output = response.getOutputStream();
    
    String acceptEncoding = request.getHeader("Accept-Encoding");
    if (acceptEncoding != null && acceptEncoding.contains("gzip")) {
        response.setHeader("Content-Encoding", "gzip");
        output = new GZIPOutputStream(output);
    }
    
    output.write(json.getBytes("UTF-8"));
    

    (note that you would like to set the content type and character encoding as well, this is taken into account in the example)

    You could also configure this at appserver level. Since it’s unclear which one you’re using, here’s just a Tomcat-targeted example: check the compression and compressableMimeType attributes of the <Connector> element in /conf/server.xml: HTTP connector reference. This way you can just write to the response without worrying about gzipping it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The request I sent is accept gzip but the response is not compressed, instead,
I am sending an Ajax Request using jQuery. What happens is that I am
If I navigate here: http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js I download 70k using Firefox 3.6.3 and I can
Well.. we've developed a j2ee application using struts2 ajax capabilities. We find that the
I am using .Net framework 2.0 / jQuery to make an Ajax call to
I have created an IHttpAsyncHandler that I'm trying to call using AJAX, with jQuery.
I'm using a jquery to make an $.ajax request. It works fine in chrome/firefox
I using jQuery Ajax like below: $.ajax({ url: 'servlet/*****Servlet', dataType: text, success: function(data) {
I'm trying to create a simple AJAX (via jQuery) request to http:// yourusername .couchone.com/
If webserver can send gzip response, why can't browser sent gzip request?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.