Search code examples
javaservletsresponseservlet-filtersdeflate

Java deflate response


Hello all I want to make a filter for tomcat to deflate all responces of certain MIME type. Any guidelines?

...
 String ae = request.getHeader("accept-encoding");
        if (ae != null && ae.indexOf("deflate") != -1) {
            deflate response...?????
        }
chain.doFilter(request, res);

Solution

  • Don't do that in a homebrewed Filter. Configure it at server level. In case of for example Apache Tomcat, just add compression="on" to <Connector> element in /conf/server.xml. It will GZIP responses whenever client accepts it (GZIP is based on deflate and practically every client supports it whenever deflate is supported).

    <Connector compression="on">
    

    That's all. You can if necessary configure mime types by compressableMimeType attribute.

    See also: