December 13, 2013 | Posted in:tools, Uncategorized

How to prevent ‘nocache.js’ or cache.html from getting cached in Browser when we build Google Web toolkit applications ?
This need arises when you are releasing a newer version of an existing application to existing users. Any user who has visited the GWT application before hand would have cached the previous version files and in all likelihood won’t see the new version changes.
GWT (Google web toolkit ) applications do not work properly in some browsers unless the user refresh the page cache forcefully. If a browser has an older version of this js file or more critical cache.html file cached in the client browser, the application will not be able to properly communicate with server code or render the newer version and most of the time behaves erratically and remains unpredicatable.
Solution for this is to make sure that the older version files specially ‘nocache.js’  & cache.html are not cached by the browser.
 The string in the filename i.e. nocache.js does allow to configure your server to send HTTP header information to the browser to stop it from caching.
To achieve this  In your Java application you have to create a Filter to dispatch the necessary HTTP header information and you can skip the file from getting cached in browser.
Following is the simple Http Filter code:

public class CacheControllerFilter implements Filter{
 
private Logger logger = Logger.getLogger(CacheControllerFilter.class.getSimpleName());
public void destroy() {
}
 
public void init(FilterConfig config) throws ServletException {
}
 
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain){
logger.log(Level.INFO, “[CahcheControllerFilter] in doFilter() method…”);
try {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String requestURI = httpServletRequest.getRequestURI();
if (requestURI.contains(“.nocache.”)) {
Date currentDate = new Date();
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
httpServletResponse.setDateHeader(“Date”, currentDate.getTime());
// one day old
httpServletResponse.setDateHeader(“Expires”, currentDate.getTime() – 86400000L);
httpServletResponse.setHeader(“Pragma”, “no-cache”);
httpServletResponse.setHeader(“Cache-control”, “no-cache, no-store, must-revalidate”);
}
filterChain.doFilter(request, response);
} catch (Exception ex) {
logger.log(Level.INFO, “[CahcheControllerFilter] Exception in doFilter() method…”,ex);
}
}
 
Headers to be set:
Expires:             Specifies time when the document may change or when its information will become invalid. It shows that it is unlikely the document will change before that time.
Cache-control: Specifies any special treatment a caching system should give to this document. The most common values are no-cache (to indicate this document should not be cached), no-store (to indicate this document should not be cached or even stored by a proxy server, usually due to its sensitive contents), and max-age= seconds (to indicate how long before the document should be considered stale).
 
Pragma:   equivalent of Cache-control, with no-cache as its only possible value.
 
Basically this filter checks whether the http request URI contains the string ”.nocache.”, if it contains then it sets the header to tell the browser to not cache the file, and if it does not then set the Header as it is. Add the extra steps if you want to set header for other files such as cache.html
And  you have to put mapping of this filter in your web.xml file:
<filter>
<filter-name>cacheControlFilter</filter-name>
<filter-class>com.example.server.service.CacheControllerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cacheControlFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

 

email