fc.web.servlet
Class GzipFileServlet

java.lang.Object
  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:

<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:
Serialized Form

Constructor Summary
GzipFileServlet()
           
 
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
service
 
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

GzipFileServlet

public GzipFileServlet()
Method Detail

init

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

doGet

public void doGet(javax.servlet.http.HttpServletRequest req,
                  javax.servlet.http.HttpServletResponse res)
           throws javax.servlet.ServletException,
                  IOException
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"

Overrides:
doGet in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
IOException