Class GzipFileServlet

All Implemented Interfaces:
Servlet, ServletConfig, Serializable

public final class GzipFileServlet extends 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:

<servlet>
  <servlet-name>gzip</servlet-name>
  <servlet-class>fc.web.servlet.GzipFileServlet</servlet-class>
</servlet>
...
<!-- an example mapping, your taste may vary -->
<servlet-mapping>
  <servlet-name>gzip</servlet-name>
  <url-pattern>/gzip</url-pattern>
</servlet-mapping>
Then, in this example, one can say, in your HTML document:
<html>
<head>
  <title>Untitled</title>
  <script src="/gzip?js=myscript.js"></script>
</head>
<body>
hello
</body>
</html>
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:
    file
    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]
    mimetype
    The mimetype of the retrieved file (after it has been gunzipped). This will commonly be text/html or text/javascript
    encoding
    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:
    js
    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: