public class GzipResponseFilter extends HttpFilter
While GZIP is normally to be configured in the servlet container (e.g.
in Tomcat, or
<property name="compression" value="on"> in Glassfish), this filter allows a
servlet container independent way of configuring GZIP compression and also allows enabling GZIP compression anyway
on 3rd party hosts where you have no control over servlet container configuration.
To get it to run, map this filter on the desired
<url-pattern> or maybe even on the
<servlet-name> of the
Filter is by default dispatched
REQUEST only, you might want to explicitly add the
ERROR dispatcher to get it to run
on error pages as well.
<filter> <filter-name>gzipResponseFilter</filter-name> <filter-class>org.omnifaces.filter.GzipResponseFilter</filter-class> </filter> <filter-mapping> <filter-name>gzipResponseFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>
This filter supports two initialization parameters which needs to be placed in
threshold is thus 500 bytes. This means that when the response is not larger than 500 bytes,
then it will not be compressed with GZIP. Only when it's larger than 500 bytes, then it will be compressed. A
threshold of between 150 and 1000 bytes is recommended due to overhead and latency of compression/decompression.
The value must be a number between 0 and 9999. A value larger than 2000 is not recommended.
mimetypes represents a comma separated string of mime types which needs to be compressed. It's
exactly that value which appears in the
Content-Type header of the response. The in the above example
mentioned mime types are already the default values. Note that GZIP does not have any benefit when applied on
binary mimetypes like images, office documents, PDF files, etcetera. So setting it for them is not recommended.
|Constructor and Description|
|Modifier and Type||Method and Description|
Perform the filtering job.
Initializes the filter parameters.
public void init() throws ServletException
public void doFilter(HttpServletRequest request, HttpServletResponse response, HttpSession session, FilterChain chain) throws ServletException, IOException
GzipHttpServletResponseand pass it through the filter chain.
Filter.doFilter(ServletRequest, ServletResponse, FilterChain)