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.
> -----Original Message-----
> From: A mailing list for discussion about Sun Microsystem's
> Java Servlet
> API Technology. [mailto:[EMAIL PROTECTED]]On
> Behalf Of Heiko
> Grussbach
> Sent: Monday, August 09, 1999 8:36 AM
> To: [EMAIL PROTECTED]
> Subject: Re: Compressing the output of a servlet
>
>
> Hi Andr�s,
>
> I havent used compression myself, but info may be found in J. Hunters
> servlet book on page 189. According to this, you need to set
> the content
> header of the response to x-gzip or x-compress (not just to
> gzip as you
> do).
> Also, revise your check for the the header, it might return
> null in which
> case your first line would fail with a NullPointerException.
> Please tell me if you get it working!!
>
> Regards
>
> Heiko
> [EMAIL PROTECTED]
>
>
>
>
>
> Andr�s Aguiar <[EMAIL PROTECTED]> on 07-08-99 11:53:47
>
> Please respond to "A mailing list for discussion about Sun
> Microsystem's
> Java Servlet API Technology."
> <[EMAIL PROTECTED]>
>
> To: [EMAIL PROTECTED]
> cc: (bcc: Heiko Grussbach/NMG/CRP-HT)
>
> Subject: Compressing the output of a servlet
>
>
>
> Content-type: text/plain; charset
>
> I'm trying to send the output of my servlet compressed to the browser,
> depending on the 'Accept-Encoding' header in the HTTP request.
> I've followed the HTTP1.1 spec, but it seems that I'm doing something
> wrong.
> This is the code I'm using:
> //-----------------------------
> // htmlOut is a StringBuffer that has the data to send to the browser.
> if (req.getHeader("Accept-Encoding").indexOf("gzip") >{
> res.setHeader("Content-Encoding", "gzip");
> res.setHeader("Transfer-Encoding","chunked");
> res.getOutputStream().write(new
> String(Long.toHexString(htmlOut.length()).toUpperCase() +
> "\r\n").getBytes());
> GZIPOutputStream gzOut
> GZIPOutputStream(res.getOutputStream());
> gzOut.write(htmlOut.toString().getBytes());
> gzOut.close();
> res.getOutputStream().write(new
> String("\r\n0\r\n\r\n").getBytes());
> res.getOutputStream().close();
> }
> //---------------------
> If someone was able to do it, send me a clue.
> 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
>
> ______________________________________________________________
> _____________
> 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
>
>
___________________________________________________________________________
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