Archive

Posts Tagged ‘caching’

Servlet Filter to prevent No-Cache

March 27th, 2011 No comments

In one of the posts, I had talked about IE caching AJAX calls. To solve this issue , you would have to modify response such that it informs browser not to cache the data coming from this server.

I am explaining it in plain old fashioned way of ServletFilter.

Below is the code for ServletFilter.

public class NoCacheFilter implements Filter {

private FilterConfig filterConfig = null;

public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

public void destroy() {
this.filterConfig = null;
}

/**
* Modifies the response. Sets the appropriate headers
* and invoke the next filter in the chain:
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

HttpServletResponse httpResponse = (HttpServletResponse) response;

httpResponse.setHeader(“Cache-Control”, “No-cache”);
httpResponse.setDateHeader(“Expires”, 0);
httpResponse.setHeader(“Pragma”, “No-cache”);
chain.doFilter(request, response);

}
}

This is helpful for browsers like IE which Cache your data by default and do not display fresh data. This is one of the situations when you are working with JSON REST web services in which you might to pull and display fresh data.

Categories: Uncategorized Tags:

IE caching Ajax calls

March 2nd, 2011 No comments

If you have been trying to make a REST call from your javascript or jsp page, you might have found that in firefox(FF) and Safari there is a call made to server every time . But this does not happen in IE.

This is the scenario I had

I had script file client.js which was making a rest call to get some data.

First time I could see the results but then with every other subsequent call, there was no change in state, although the data should have changed.

Now this can be fixed by multiple ways

Option 1:

Change the browser settings. To  do this you have to go to

General–Settings>

and change the settings from Automatically to Every Visit to page

This will resolve the issue.

Option 2:

Change from GET to POST

Option 3 :

Add a timestamp at end of url which will looking at which server will think it is a new URL.

Read more…

Categories: Uncategorized Tags: