File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes HTML Compression - what is it? Tools/libs? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTML Compression - what is it? Tools/libs?" Watch "HTML Compression - what is it? Tools/libs?" New topic

HTML Compression - what is it? Tools/libs?

Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
Howdy folks! Just was browsing [url][url] and they mentioned they were using a technique called "HTML Compression" to speed up page download time. Can anyone explain the technique, or at least tell me where I could find products or (preferrably) libraries or example code on how to do this? They mentioned a forum index page that used to take 30 seconds to load over 56k modem now takes only 5 seconds! I've certainly got a few large pages that could do with this speedup!

CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
Andre Moo

Joined: Sep 29, 2001
Posts: 22
HTTP Compression is a method of sending information to the browser which has been compressed with the gzip deflate algorithm.
You achieve this by chaining the response OutputStream with a GZIPOutputStream in your Servlets e.g
OutputStream out = response.getOutputStream();
writer = new PrintWriter(new GZIPOutputStream(out),false);
//set the header
//your normal servlet output goes here...e.g.
writer.println("Compressed Hello World!");
This only works with browsers that can accept the compressed information. To find out if the browser does you check Accept-Encoding to see if there is an entry for gzip by inspecting request.getHeader("Accept-Encoding");
You'll be UNHAPPY to know that not all browsers support this feature!!! Especially if you want to use this for the same company as your other post.
Early versions of Netscape on Win platform and IE on non-Win platforms generally don't support this apparently, but it doesn't matter, you just use an if statement in the Servlet and depending on whether they have an appropriate "Accept-Encoding", you use the GZipped Stream or not. Make Sense?
Using HTTP Compression only really helps with larger page sizes - you have to remember there is a price to pay in getting smaller page sizes - the time it takes to perform the compression at the server and the time it takes to decompress in the browser.
Okey doke, confused? Well, lets make things really simple. You can buy the book "Core Servlets and JavaServer Pages" by Marty Hall (ISBN can't-be-bothered-to-look) and look at Chapter 4.
Or if you are really cheap and lazy you can download an example from the website
(HINT:You're looking for Chapter 4
OKAY! OKAY! Here's a bloody direct link to the source code of the servlet in question:
Some people are so lazy!
No rest for the wicked.
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
Thanks for all the info! That's just what I was looking for! I'll have to give it a try and clock some load times. Any idea if Mozilla or the Linux browsers(such as KDE browser) do gzip?
I agree. Here's the link:
subject: HTML Compression - what is it? Tools/libs?
It's not a secret anymore!