Java – the best method to GZIP a JSF-Seam web app pages

gzipjavajsfseam

I'm developing an JSF web app on Tomcat, planning to use Seam in the near future, and I want to add compression of our web pages and resources (i.e. Javascript & CSS files). I'm aware of three methods to GZIP responses in a Java web :

  1. Use Ehcache GZIP filter: it's used in Appfuse, so it's probably solid and it checks if the user agent supports GZIP before applying it, but it seems to have problems with Seam, which we will be using http://seamframework.org/Community/EHCacheGZipFilterIncompatibleWithSeam.

  2. Use pjl-filter. From the stackoverflow question: Tomcat Compression Does Not Add a Content-Encoding: gzip in the Header, it appears it doesn't have any memory leaks, but I don't know if it has problems with Seam or not.

  3. Use Tomcat's built in compression – although it may not provide a content encoding (Tomcat 6.0.14 seems to work fine, but you can only provide a black list for what user agents compression should not be applied to.

Does anyone have experience with these methods in a JSF-Seam environment? Which is the "best" solution?

Thanks,
Glen

Best Answer

GZIP filter will reduce the initial load time significantly.
You can additionally implement a cacheFilter to bring performance of your screens at par with JavaScript based UI (https://stackoverflow.com/a/35567540/5076414).
For client side components, you can use Primefaces which is JQuery based UI.

Enable GZIP filter in JSF

Simply add this to your

web.xml

<filter>
    <filter-name>gzipResponseFilter</filter-name>
    <filter-class>org.omnifaces.filter.GzipResponseFilter</filter-class>
    <init-param>
        <description>The threshold size in bytes. Must be a number between 0 and 9999. Defaults to 150.</description>
        <param-name>threshold</param-name>
        <param-value>150</param-value>
    </init-param>
    <init-param>
        <description>The mimetypes which needs to be compressed. Must be a commaseparated string. Defaults to the below values.</description>
        <param-name>mimetypes</param-name>
        <param-value>
     text/plain, text/html, text/xml, text/css, text/javascript, text/csv, text/rtf,
     application/xml, application/xhtml+xml, application/x-javascript, application/javascript, application/json,
     image/svg+xml, image/gif, application/x-font-woff, application/font-woff2, image/png
 </param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>gzipResponseFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/</location>
</error-page>   

And the following to your

pom.xml

    <dependency>
        <groupId>org.omnifaces</groupId>
        <artifactId>omnifaces</artifactId>
        <version>1.11</version>
    </dependency>

How to verify if my screen is using gzip

To see if your contents are already usign gzip and cache, In your Google Chrome Browser -> right click on your screen -> inspect -> click network tab -> refresh your screen. Click on the images, icons, stylesheets and see if you see following in response header

Content-Encoding:gzip if the status of element is 200

Related Topic