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
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:
Then, in this example, one can say, in your HTML document:<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>
That will load the<html> <head> <title>Untitled</title> <script src="/gzip?js=myscript.js"></script> </head> <body> hello </body> </html>
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.
-
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
ortext/javascript
- encoding
- The text encoding of the specified file. Commonly, it is US-ASCII, ISO-8859-1 or UTF-8
/gzip?file=foo.html.gzinvalid input: '&mimetype'=text/htmlinvalid input: '&encoding'=ISO-8859-1
-
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 -
Method Summary
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
-
Constructor Details
-
GzipFileServlet
public GzipFileServlet()
-
-
Method Details
-
init
- Specified by:
init
in interfacejakarta.servlet.Servlet
- Overrides:
init
in classjakarta.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
-