Click here to Skip to main content
15,891,657 members
Articles / Programming Languages / XML
Technical Blog

Improve performance by caching and compression

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
5 Jul 2013CPOL2 min read 8.2K   4   1
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.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionHow to use this functionality with asp.net ? Pin
Shailesh vora8-Aug-13 19:03
Shailesh vora8-Aug-13 19:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.