This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTML Compression - what is it? Tools/libs?" Watch "HTML Compression - what is it? Tools/libs?" New topic
Author

HTML Compression - what is it? Tools/libs?

Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
Howdy folks! Just was browsing [url]www.aceshardware.com/[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!
Thanks!


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

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
response.setHeader("Content-Encoding","gzip");
//your normal servlet output goes here...e.g.
writer.println("Compressed Hello World!");
writer.close()
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 http://www.coreservlets.com
(HINT:You're looking for Chapter 4 EncodedPage.java)
OKAY! OKAY! Here's a bloody direct link to the source code of the servlet in question:http://archive.coreservlets.com/coreservlets/EncodedPage.java
Some people are so lazy!
No rest for the wicked.
Andre.
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?
 
jQuery in Action, 2nd edition
 
subject: HTML Compression - what is it? Tools/libs?