Web page designs are becoming innovative with rich interface which involve extra code such as JavaScript, CSS, and images etc. Most of the end-user response time tied-up in downloading these components. Optimization of the number of HTTP requests and response size are the key parameters to improve web application performance.
This article explains caching filter and compression filter that are suitable for use with any web application to optimize the number of requests and the response size.
Compressing Content Gzip Servlet Filter
HTTP Compression is a way to compress content transferred from servers to browsers which reduce the http response size. This standards-based method of delivering compressed content is built into HTTP/1.1, and all modern Web browsers support the HTTP/1.1 protocol, i.e. they can decode compressed files automatically at the client-side browser. The smaller the size of your information, the faster it can all be sent. Therefore, if you compress the content your web application, it will be displayed on the user’s screen faster.
Source Code:Download link OpenWebOptimizer for Gzip Servlet filter source code with sample code and usage document.
Gzip is the most popular and effective compression method at this time. It was developed by the GNU project and standardized by RFC 1952. Gzipping generally reduces the response size by about 70%.
For adding the Gzip filter just adds below code into web.xml including attached Gzip filters
<filter>
<filter-name>GZIPFilter</filter-name>
<filter-class>com.opcat.gzip.GZIPFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GZIPFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>]]>
HTTP Servlet Caching:
Caching reduces number of HTTP request which makes web page faster. Web application generate browser content and in many scenario content won’t change between different requests therefore if you can cache the response ,you can reuse again without HTTP requests which improve the web application performance. We can achieve caching by just writing simple caching filter
Source Code: Download link OpenWebOptimizer for Caching Servlet filter source code with sample code and usage document.
Caching filter means a servlet that will intercept all requests and cache it and have a valid cached copy. The filter will immediately respond to the requests by sending a copy of cached contents. However, if no cache exists, the filter will pass
the request on to its intended endpoint, and the response will be generated and cached for future request.
For adding the caching filter just add below code into web.xml:
<filter>
<filter-name>jsCache</filter-name>
<filter-class>com.opcat.cache.CacheFilter</filter-class>
<init-param>
<param-name>private</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>expirationTime</param-name>
<!-- Change this to add the expiry time for re-validating the files -->
<param-value>0</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jsCache</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
Summary:
Caching and Compression filters optimize HTTP request call, contents size and contents generation. Caching and Compression are most important task in terms of web application performance. We can use attached project which is free and open source to use in your application.