5.4 Sending Compressed Web Pages

Gzip is a text compression scheme that can dramatically reduce the size of HTML (or plain text) pages. Most recent browsers know how to handle gzipped content, so the server can compress the document and send the smaller document over the network, after which the browser will automatically reverse the compression (no user action required) and treat the result in the normal manner. Sending such compressed content can be a real time saver since the time required to compress the document on the server and then uncompress it on the client is typically dwarfed by the time saved in download time, especially when dialup connections are used.

DILBERT reprinted by permission of United Feature Syndicate, Inc.

graphics/05inf01.gif

However, although most recent browsers support this capability, not all do. If you send gzipped content to browsers that don't support this capability, the browsers will not be able to display the page at all. Fortunately, browsers that support this feature indicate that they do so by setting the Accept-Encoding request header. Browsers that support content encoding include most versions of Netscape for Unix, most versions of Internet Explorer for Windows, and Netscape 4.7 and later for Windows. Earlier Netscape versions on Windows and Internet Explorer on non-Windows platforms generally do not support content encoding.

Listing 5.2 shows a servlet that checks the Accept-Encoding header, sending a compressed Web page to clients that support gzip encoding (as determined by the isGzipSupported method of Listing 5.3) and sending a regular Web page to those that don't. The result (see Figure 5-3) yielded a compression of over 300 -fold and a speedup of more than a factor of 10 when a dialup connection was used. In repeated tests with Netscape and Internet Explorer on a 28.8K modem connection, the compressed page averaged less than 5 seconds to completely download, whereas the uncompressed page consistently took more than 50 seconds. Results were less dramatic with faster connections, but the improvement was still significant. Gzip compression is such a useful technique that we later present a filter that lets you apply gzip compression to designated servlets or JSP pages without changing the actual code of the individual resources. For details, see the chapter on servlet and JSP filters in Volume 2 of this book.

Figure 5-3. Since the Windows version of Internet Explorer 6 supports gzip, this page was sent gzipped over the network and automatically reconstituted by the browser, resulting in a large saving in download time.

graphics/05fig03.jpg

Core Tip

graphics/bwopenglobe_icon.gif

Gzip compression can dramatically reduce the download time of long text pages.


Implementing compression is straightforward since support for the gzip format is built in to the Java programming language by classes in java.util.zip . The servlet first checks the Accept-Encoding header to see if it contains an entry for gzip. If so, it uses a PrintWriter wrapped around a GZIPOutputStream and specifies gzip as the value of the Content-Encoding response header. If gzip is not supported, the servlet uses the normal PrintWriter and omits the Content-Encoding header. To make it easy to compare regular and compressed performance with the same browser, we also added a feature whereby we can suppress compression by including ?disableGzip at the end of the URL.

Listing 5.2 LongServlet.java
 package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Servlet with <B>long</B> output. Used to test  *  the effect of the gzip compression.  */ public class LongServlet extends HttpServlet {   public void doGet(HttpServletRequest request,                     HttpServletResponse response)       throws ServletException, IOException {     response.setContentType("text/html");  // Change the definition of "out" depending on whether   // or not gzip is supported.   PrintWriter out;   if (GzipUtilities.isGzipSupported(request) &&   !GzipUtilities.isGzipDisabled(request)) {   out = GzipUtilities.getGzipWriter(response);   response.setHeader("Content-Encoding", "gzip");   } else {   out = response.getWriter();   }  // Once "out" has been assigned appropriately, the     // rest of the page has no dependencies on the type     // of writer being used.     String docType =       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +       "Transitional//EN\">\n";     String title = "Long Page";     out.println       (docType +        "<HTML>\n" +        "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +        "<BODY BGCOLOR=\"#FDF5E6\">\n" +        "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n");     String line = "Blah, blah, blah, blah, blah. " +                   "Yadda, yadda, yadda, yadda.";     for(int i=0; i<10000; i++) {       out.println(line);     }     out.println("</BODY></HTML>");  out.close(); // Needed for gzip; optional otherwise.  } } 
Listing 5.3 GzipUtilities.java
 package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.zip.*; /** Three small static utilities to assist with gzip encoding.  *  <UL>  *    <LI>isGzipSupported: does the browser support gzip?  *    <LI>isGzipDisabled: has the user passed in a flag  *        saying that gzip encoding should be disabled for  *        this request? (Useful so that you can measure  *        results with and without gzip on the same browser).  *    <LI>getGzipWriter: return a gzipping PrintWriter.  *  </UL>  */ public class GzipUtilities {   /** Does the client support gzip? */   public static boolean isGzipSupported       (HttpServletRequest request) {  String encodings = request.getHeader("Accept-Encoding");   return((encodings != null) &&   (encodings.indexOf("gzip") != -1));  }   /** Has user disabled gzip (e.g., for benchmarking)? */   public static boolean isGzipDisabled       (HttpServletRequest request) {     String flag = request.getParameter("disableGzip");     return((flag != null) && (!flag.equalsIgnoreCase("false")));   }   /** Return gzipping PrintWriter for response. */   public static PrintWriter getGzipWriter       (HttpServletResponse response) throws IOException {     return(new PrintWriter             (new GZIPOutputStream               (response.getOutputStream())));   } } 


Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net