Class GZIPFilter
- java.lang.Object
-
- com.twelvemonkeys.servlet.GenericFilter
-
- com.twelvemonkeys.servlet.gzip.GZIPFilter
-
- All Implemented Interfaces:
java.io.Serializable
,javax.servlet.Filter
,javax.servlet.FilterConfig
@Deprecated public class GZIPFilter extends GenericFilter
Deprecated.A filter to reduce the output size of web resources.The HTTP protocol supports compression of the content to reduce network bandwidth. The important headers involved, are the
Accept-Encoding
request header, and theContent-Encoding
response header. This feature can be used to further reduce the number of bytes transferred over the network, at the cost of some extra processing time at both endpoints. Most modern browsers supports compression in GZIP format, which is fairly efficient in cost/compression ratio.The filter tests for the presence of an
Accept-Encoding
header with a value of"gzip"
(several different encoding header values are possible in one header). If not present, the filter simply passes the request/response pair through, leaving it untouched. If present, theContent-Encoding
header is set, with the value"gzip"
, and the response is wrapped. The response output stream is wrapped in aGZIPOutputStream
which performs the GZIP encoding. For efficiency, the filter does not buffer the response, but writes through the gzipped output stream.Configuration
To useGZIPFilter
in your web-application, you simply need to add it to your web descriptor (web.xml
). If using a servlet container that supports the Servlet 2.4 spec, the newdispatcher
element should be used, and set toREQUEST/FORWARD
, to make sure the filter is invoked only once for requests. If using an older web descriptor, set theinit-param
"once-per-request"
to"true"
(this will have the same effect, but might perform slightly worse than the 2.4 version). Please see the examples below. Servlet 2.4 version, filter section:
<!-- GZIP Filter Configuration --> <filter> <filter-name>gzip</filter-name> <filter-class>com.twelvemonkeys.servlet.GZIPFilter</filter-class> </filter>
Filter-mapping section:
<!-- GZIP Filter Mapping --> <filter-mapping> <filter-name>gzip</filter-name> <url-pattern>*.html</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <filter-mapping> <filter-name>gzip</filter-name> <url-pattern>*.jsp< /url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
Based on ideas and code found in the ONJava article Two Servlet Filters Every Web Application Should Have by Jayson Falkner.
- Version:
- $Id: GZIPFilter.java#1 $
- Author:
- Jayson Falkner, Harald Kuhr, last modified by $Author: haku $
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class com.twelvemonkeys.servlet.GenericFilter
oncePerRequest
-
-
Constructor Summary
Constructors Constructor Description GZIPFilter()
Deprecated.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
doFilterImpl(javax.servlet.ServletRequest pRequest, javax.servlet.ServletResponse pResponse, javax.servlet.FilterChain pChain)
Deprecated.Invoked once, or each time a request/response pair is passed through the chain, depending on theGenericFilter.oncePerRequest
member variable.-
Methods inherited from class com.twelvemonkeys.servlet.GenericFilter
destroy, doFilter, getFilterConfig, getFilterName, getInitParameter, getInitParameterNames, getServletContext, init, init, log, log, setFilterConfig, setOncePerRequest
-
-
-
-
Method Detail
-
doFilterImpl
protected void doFilterImpl(javax.servlet.ServletRequest pRequest, javax.servlet.ServletResponse pResponse, javax.servlet.FilterChain pChain) throws java.io.IOException, javax.servlet.ServletException
Deprecated.Description copied from class:GenericFilter
Invoked once, or each time a request/response pair is passed through the chain, depending on theGenericFilter.oncePerRequest
member variable.- Specified by:
doFilterImpl
in classGenericFilter
- Parameters:
pRequest
- the servlet requestpResponse
- the servlet responsepChain
- the filter chain- Throws:
java.io.IOException
- if an I/O error occursjavax.servlet.ServletException
- if an exception occurs during the filter process- See Also:
GenericFilter.oncePerRequest
,doFilter
,Filter.doFilter
-
-