0 Replies Latest reply: Aug 27, 2011 9:45 AM by 869772 RSS

    Content Caching: Servlet.getLastModified()

    869772
      Hello everyone,

      I've been experimenting the past few days in the process of caching the contents of a servlet response, at the client side, and it was a very frustrating experience. After I was able to get a hold of all the mechanics of this process, I decided to share that information here, to relieve whoever is interested from the mess and frustration I've been through, especially that I haven't found much details on the topic in this forum and on the internet in general.

      In case of the GET http method, the default implementation of the service() method uses the getLastModified() method to determine weather the servlet supports the idea of "client-side caching" or not. If getLastModified() returns -1 (which is the default behavior) then the service() method determines that caching is not supported by this servlet, and immediately passes control the the doGet() method, to construct the response for the client.

      In order to support content caching, one has to override the getLastModified() method to return the date at which the servlet's-response content has been last modified. When the service() method finds that a positive value is returned by the getLastModified() method, one of the following scenarios would take place.

      1-If the request coming from the client doesn't contain the If-Modified-Since header, then the service() method would add the Last-Modified header to the response object, specifying the date when the last content-modification took place, then pass control to the doGet() method. The client would then cache that response, take the value from the Last-Modified header and put it in the If-Modified-Since header in all the requests that follow, to that servlet.

      2-If the request coming from the client does contain the If-Modified-Since header with a date EQUAL-TO the one returned by the getLastModified() method, then the service() method responds with a 304 (NOT MODIFIED) status to the client without referring to the doGet() method, and the client should use the cached content. Notice that in this case the service() method would NOT include the Last-Modified header to the response, since the client already knows it.

      3-If the request coming from the client does contain the If-Modified-Since header with a date EARLIER-THAN the one returned by the getLatModified() method, then the service() method would add the Last-Modified header to the response object, specifying the date when the last content-modification took place, then pass control to the doGet() method. The client would then cache that response,(which is newer than the one it was already caching) and take the value from Last-Modified header and put it in the If-Modified-Since header in all the requests that follow.

      One last thing, when you set the modification date, you HAVE TO round it down to the nearest second ( date / 1000 * 1000), as determined by the time precision in communications over the HTTP: if this process is left out, responses will NEVER be cached.

      Edited by: 866769 on Aug 27, 2011 7:40 AM