The Kat's Work - Blog
Main | Blog | Registration | Login
Saturday
2024-04-20
7:54 AM
Welcome Guest | RSS
Main » 2009 » November » 16

For me one of the biggest improvements you can make is adding expires headers where necessary. Lets reiterate that point. Where necessary!!!


A quick explanation, modern browsers cache files so that they don't have to be downloaded every time. That isn't the end of the story without an expires header the browser will still check if a new version of the file exists. Say you have 5 javascript files, 5 css files, 40 png images. Thats 50 unnecessary http requests and, lets be honest, unless you have gone to town on your optimisation the above is quite a simple page.


Expires headers tell your browser that a file is valid until a certain date. So your browser does not check for new versions until that date is reached. If we set them correctly the page above will make no unnecessary httprequests and because of that be a lot faster at loading.


Superb, so lets go crazy with adding them than....no! The expires header strategy has some pretty major impacts on your development. Any changes that you make to the file before the header expires will not replace those already on users browsers. The obvious solution to this is to use versioning in your file names. Change the file name from test_1_0_1.js to test_1_0_2.js and the browser sees it as a new file a downloads it, you do have to remember to change any referencing code as well. Also what about images that are user/admin changeable you have to make sure that these files a versioned as well. As you can see for some projects this is a lot of hassle to make a speed saving, so its important to only use Expires Headers where necessary. Phew!


Ok onto the meat and bones of it how do we implement in Java Spring. Simple first off add the following class to your project.


/**

* CacheFilter.java

*

* Created on 9 Dec 2008,11:06:17

*/

package com.uk.spree4.utils;


import java.io.IOException;

import java.util.Date;


import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

* Adds an expires header to paths defined in the web.xml

* @author

*/

public class CacheFilter implements Filter{

FilterConfig filterC;


/* (non-Javadoc)

* @see javax.servlet.Filter#destroy()

*/

public void destroy() {

this.fc = null;

}


/* (non-Javadoc)

* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)

*/

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

String path =

filterC.getInitParameter(((HttpServletRequest)request).getRequestURI());

if (path!= null && path.equals("nocache")) {

chain.doFilter(request, response);

return;

}


long today = new Date().getTime();

HttpServletResponse httpResponse = (HttpServletResponse)response;

httpResponse.setDateHeader("Expires", today+61536000000L);

chain.doFilter(request, response);

}


/* (non-Javadoc)

* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)

*/

public void init(FilterConfig arg0) throws ServletException {

this.fc = arg0;

}


}


So all files passed through the filter that don't have the structure words nocache (i.e test.nocache.js), will be stamped with a far future Expires header.


Now in your web.xml set up your filter. As in the example below.


<filter>

<filter-name>CacheFilter</filter-name>

<filter-class>org.your.structure.CacheFilter</filter-class>

</filter>


<filter-mapping>

<filter-name>CacheFilter</filter-name>

<url-pattern>/jQuery/*</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>CacheFilter</filter-name>

<url-pattern>/images/*</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>CacheFilter</filter-name>

<url-pattern>/Scripts/*</url-pattern>

</filter-mapping>


As you can see all the files in the jquery, images and scirpts folders will be passed through the filter.


Well I hope this helps someone.




Views: 1087 | Added by: The_Kat | Date: 2009-11-15 | Comments (1)

Login form
Adverts
Search
Calendar
«  November 2009  »
SuMoTuWeThFrSa
1234567
891011121314
15161718192021
22232425262728
2930
Entries archive
Site friends
  • Create your own site
  • Spree4.com
  • My Blog
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    spree4
    Copyright MyCorp © 2024
    Website builderuCoz