Class GzipFileServlet

  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by fc.web.servlet.GzipFileServlet
All Implemented Interfaces:
Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public final class GzipFileServlet
extends javax.servlet.http.HttpServlet

Sends the specified file as a binary stream after setting the ContenEncoding to be gzip. (the specified file should have a .gz extension, this is mandated because it helps catch user errors, for example forgetting to gzip the file).

Note, this servlet does not Gzip data, it only serves data that has already been gzip'ed.

To use, add something like the following to web.xml:

<!-- an example mapping, your taste may vary -->
Then, in this example, one can say, in your HTML document:
        <script src="/gzip?js=myscript.js"></script>
That will load the myscript.js.gz file (and if myscript.js.gz is not present, a servlet exception will be logged). Note, the ".gz" extension is added automatically if not specified in the filename.

There are two modes in serving files. In both cases, the file to be retrieved must already exist as a gzip file on the server.

  1. Retrieve a arbitrary gzip'ed file from the server. There are 3 required parameters:
    the filename (if the specified name does not have a .gz extension, ".gz" will be added automatically to the specified name). [see path note below]
    The mimetype of the retrieved file (after it has been gunzipped). This will commonly be text/html or text/javascript
    The text encoding of the specified file. Commonly, it is US-ASCII, ISO-8859-1 or UTF-8
    Example: /gzip?file=foo.html.gz&mimetype=text/html&encoding=ISO-8859-1
  2. Retrieve a Javascript file (convenience method): This will automatically set the content-type to be text/javascript; charset=utf-8 (so the mime or charset do not have to be specified). One need only specify:
    path to the gzip'ed file. [see path note below]

Path name for the specified file

This path to the included file must be in absolute (start with a '/') or relative form.

Absolute path names are like HTML absolute names and start from the document root directory of the web server.

Relative names (relative to the invoking page) are not supported by this servlet.

See Also:
Serialized Form

Constructor Summary
Method Summary
 void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
          Returns the specified gzip file.
 void init(javax.servlet.ServletConfig conf)
Methods inherited from class javax.servlet.http.HttpServlet
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public GzipFileServlet()
Method Detail


public void init(javax.servlet.ServletConfig conf)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Servlet
init in class javax.servlet.GenericServlet


public void doGet(javax.servlet.http.HttpServletRequest req,
                  javax.servlet.http.HttpServletResponse res)
           throws javax.servlet.ServletException,
Returns the specified gzip file. If the specified name does not end with ".gz", ".gz" will automatically be tacked on to the specified name (so files served by this method must always end with ".gz"

doGet in class javax.servlet.http.HttpServlet