Enable caching in Tomcat 6?

I need to increase page loading speed. On google page speed, I have this problem:

The following cached resources have a short freshness life. Indicate at least one week in the future for the following resources.

Therefore, I have to add an expiration date to the header in order to force the browser to cache static content on the page. Is there any solution for this?

I am using tomcat 6.0.26.

0
source share
2 answers

One solution using spring framework

You need to write a filter similar to this:

@WebFilter(dispatcherTypes = { YourDispatcherTypes }, urlPatterns = { "*.jsp","/yourresourcename/*", "oranyother"})
public class CacheHandlingFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain)
        throws ServletException, IOException {

    HttpServletRequest httpReq = (HttpServletRequest) request;

    HttpServletResponse httpResp = (HttpServletResponse) response;

    if(httpReq.getRequestURI().contains("/yourresourcename/")) {
        httpResp.setDateHeader("Expires", ProvideTimeForCacheHere);
        httpResp.setHeader("Cache-Control", "public, max-age=" + ProvideTimeForCacheHere);
    }

    filterChain.doFilter(request, response);
}
}
+2
source

, - , .

Tomcat 7 ExpiresFilter ; Tomcat 6 .

+1

All Articles