Andres: thanks for posting this. I'd given up on it a while back as too
unsupported by the browsers as it behaved erratically, but you seem to have
cracked it :). One suggestion I would make is that you explicitly set the
Content-Type header using res.setContentType("text/html or whatever"). I found
that NS 4.6 (Win95) would treat all files as .txt otherwise.
Additionally, Netscape cannot handle gzipped gifs whereas IE5 does - it may seem
pointless compressing a gif, but I'm actually getting up to 6x compression on
them!!
For any doubters of using compression out there, especially those who don't live
in bandwidth-cheap areas, here's a little comparison of how browsers with
compression could you save you bytes: on two pages, so 2* html files, and 4*
fairly large gifs (it was a web traffic report), from Apache's transfer logs.
Netscape 3.01 (no compression) 257k
Netscape 4.6 (compressed html) 147k
MSIE 5.0 (compressed html & gif) 43k
I remain convinced :)
- simon
AndrXs Aguiar wrote:
> I've solved the problem, so I'm posting the solution in the list because I
> think it's pretty useful.
>
> Background: HTTP request have an 'Accept-Encoding' header that tells the
> HTTP server what kind of encoding does the browser accept. One of the
> possible encodings is 'gzip'. If the browser accepts that kind of encoding,
> you can compress the output of the servlet using a GZipOutputStream, and the
> browser will decompress it on the fly.
>
> It's pretty simple to do it:
>
> // htmlOut is a StringBuffer with the data to print in the HTML page.
> res/req are the HttpServletResponse/Request
>
> String acceptEncoding = req.getHeader("Accept-Encoding");
> if (acceptEncoding != null && acceptEncoding.indexOf("gzip") >= 0)
> {
> res.setHeader("Content-Encoding", "gzip");
>
> GZIPOutputStream gzOut = new GZIPOutputStream(res.getOutputStream());
> gzOut.write(htmlOut.toString().getBytes());
> gzOut.close();
> res.getOutputStream().close();
> }
> else
> {
> res.getWriter().print(htmlOut);
> res.getWriter().close();
> }
>
> I've tried with NS 4.06, NS 4.6, IE 5 and IE 4 in WindowsNT. All of them
> report that accept the 'gzip' encoding, and they display the compressed page
> correctly. NS 3, NS 2 and Opera report that they don't support the 'gzip'
> encoding. So, it seems that they have a consistent behavior.
>
> If anyone tries this and finds a browser that is inconsistent (ie it reports
> it supports gzip but it doesn't), please let me know.
>
> Regards.
>
>
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html