Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
4.4 Sending Compressed Web Pages
105
DILBERT reprinted by permission of United Syndicate, Inc.
Explorer on non Windows platforms generally do not support content encod
ing. Fortunately, browsers that support this feature indicate that they do so
by setting the
Accept Encoding
request header. Listing 4.2 shows a servlet
that checks this header, sending a compressed Web page to clients that sup
port gzip encoding and sending a regular Web page to those that don't. The
result showed a tenfold speedup for the compressed page when a dialup con
nection was used. In repeated tests with Netscape 4.7 and Internet Explorer
5.0 on a 28.8K modem connection, the compressed page averaged less than 5
seconds to completely download, whereas the uncompressed page consis
tently took more than 50 seconds.
Core Tip
Gzip compression can dramatically reduce the download time of long text
pages.
Implementing compression is straightforward since gzip format is built in
to the Java programming languages via classes in
java.util.zip
. The serv
let first checks the
Accept Encoding
header to see if it contains an entry for
gzip. If so, it uses a
GZIPOutputStream
to generate the page, specifying
gzip
as the value of the
Content Encoding
header. You must explicitly call
close
when using a
GZIPOutputStream
. If gzip is not supported, the servlet
uses the normal
PrintWriter
to send the page. To make it easy to create
benchmarks with a single browser, I also added a feature whereby compres
sion could be suppressed by including
?encoding=none
at the end of the
URL.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved