Caching static resources in web application

Most of the Dynamic Web Applications that we create has static resources such as JavaScript file or image file. Now the best practice would be to keep these resources on the web server and let web server serve static requests but in order to keep the deployment simple we package these resources as part of the Web Application. The WebSphere Application Server has a com.ibm.ws.webcontainer.servlet.SimpleFileServlet Servlet that gets request if a request is made to static resource in the web application. You can enable caching for SimpleFileServlet so that the content of static resource is cached.

I wanted to try that so i made changes in my DynaCacheSample.war to add sample.js and enableservletcaching1.GIF files in it and then dynacache.jsp includes both these files. I changed the cachespecs.xml file of the DynacacheSample.web to add this cache-entry element


<cache-entry>
<class>static</class>
<name>com.ibm.ws.webcontainer.servlet.SimpleFileServlet.class</name>
<cache-id>
<component id="" type="pathinfo">
<required>true</required>
</component>
<component id="If-Modified-Since" type="header">
<required>false</required>
</component>
<timeout>300</timeout>
</cache-id>
</cache-entry>


You can download the DynaCacheSample.war from here

I did deploy the modified DyanCacheSample.war file on the sever and tried hitting it few times this is what i see in the CacheMonitor application



As you can see in addition to content of dynacacheservlet and dynacache.jsp, now the content of sample.js and enableservletcaching1.gif is also cached

No comments: