Using GZIPOutputStream To Compress HTML

Discussions

Web tier: servlets, JSP, Web frameworks: Using GZIPOutputStream To Compress HTML

  1. Using GZIPOutputStream To Compress HTML (4 messages)

    Hi All
      Thanks for taking time to look at my thread. I am trying to compress the HTML sent to the browser using the GZIPOutputStream class. Here is my JSP:

    <html>
    <body>
    <%@ page import="com.picsoft.html.*,
    javax.naming.*,
         java.util.zip.*"

    buffer="128kb"
    %>


    <%

    response.setHeader("Content-Encoding", "gzip");
    response.setContentType("text/html");

    GZIPOutputStream gzipStream = new GZIPOutputStream(response.getOutputStream());

    %>

    <% gzipStream.write("Hello".getBytes()); %>

    <% gzipStream.close(); %>
    </body>

    </html>



       When I view the page on IE 6, it shows a blank page. When i remove the line

     "response.setHeader("Content-Encoding", "gzip");"

      it shows the "Hello" string in a encoded way. What might be wrong? I am using weblogic as web server.

    Thanks
    George
  2. Using GZIPOutputStream To Compress HTML[ Go to top ]

    Are you trying to compress the entire HTML or just this string ? I think you should servlet for this. write just to the GZIPOutputStream and not to the response.getOutputSteram()

    David
  3. Using GZIPOutputStream To Compress HTML[ Go to top ]

    Thanks for the response David. Unfortunately our application uses JSPs and no servlets. So is there a way to compress the whole stream(not just a string) in JSP??

    George
  4. Using GZIPOutputStream To Compress HTML[ Go to top ]

    IMHO the best solution to compress the response is to use a filter servlet. You have to assign this filter in the deployment descriptor (web.xml) of your web application.

    The interface for the filter can be found at:
    Filter Interface
  5. HTTP compression, gzip[ Go to top ]

    If you have Apache, take a look at

    http://httpd.apache.org/docs-2.0/en/mod/mod_deflate.html

    http://sourceforge.net/projects/mod-gzip/

    See also:

    http://webreference.com/internet/software/servers/http/compression/