Max Cooper wrote:

You may want to write a separate servlet to serve the image data. That
allows you to implement getLastModified() and allow proper browser-caching
support, which can significantly increase the speed of your pages if the
user is likely to view the images more than once. We did this with an Action
first and since we had caching turned off, it reloaded the images every
time. Switching to a separate servlet where we implemented getLastModified()
was perceptably faster.

Perhaps Struts should allow Action-implementers to implement some kind of
getLastModified() method for this reason. Or at least to turn caching on and
off at the Action (or action-mapping) level. getLastModified() is really
useful if you have the image data (or document data, etc.) stored in a db.



Controlling this stuff at the per-Action level is a nice idea. If you're using an Action to create dynamic output already (such as when you directly stream the binary output and then return null), it's quite easy to do today -- your Action will be able to see the "If-Modified-Since" header that the browser sends, and then can decide to return a status 304 (NOT MODIFIED) if your current database stuff is not more recent.

Something along the lines of this in your Action.execute() method should do the trick:

   // When was our database data last modified?
   long dataModifiedDate = ... timestamp when database last modified ...

   // Have we sent to this user previously?
   long modifiedSince = request.getDateHeader("If-Modified-Since");
   if (modifiedSince > -1) { // i.e. it was actually specified
       if (dataModifiedDate <= modifiedSince) {
          response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
           return (null);
       }
   }

   // Set the timestamp so the browser can send back If-Modified-Since
   response.setDateHeader("Date", dataModifiedDate);

   // Now write the actual content type and data
   response.setContentType("mage/jpg");
   ServletOutputStream stream = response.getOutputStream();
   ... write out the bytes ...

   // Return null to tell Struts the response is complete
   return (null);

-Max



Craig



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to