Class GzipFileServlet

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
fc.web.servlet.GzipFileServlet
All Implemented Interfaces:
jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable

public final class GzipFileServlet extends jakarta.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.gzinvalid input: '&mimetype'=text/htmlinvalid input: '&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:
  • Field Summary

    Fields inherited from class jakarta.servlet.http.HttpServlet

    LEGACY_DO_HEAD
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    doGet(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res)
    Returns the specified gzip file.
    void
    init(jakarta.servlet.ServletConfig conf)
     

    Methods inherited from class jakarta.servlet.http.HttpServlet

    service

    Methods inherited from class jakarta.servlet.GenericServlet

    destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • init

      public void init(jakarta.servlet.ServletConfig conf) throws jakarta.servlet.ServletException
      Specified by:
      init in interface jakarta.servlet.Servlet
      Overrides:
      init in class jakarta.servlet.http.HttpServlet
      Throws:
      jakarta.servlet.ServletException
    • doGet

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