Noel J. Bergman,

I was thinking about this same thing recently.  I really like what you've done.  I 
just have one concern - your default "getLastModified()" method seems incorrect to me:

>       protected long getLastModified(HttpServletRequest request)
>       {
>               long lm = (new
> java.io.File(getServletConfig().getServletContext().getRealPat
> h(request.getS
> ervletPath())).lastModified()) / 1000L * 1000L;
>               return lm;
>       }
> 

1.  java.io.File.lastModified() already returns a long.  Why are you dividing and 
multiplying by 1000L?

2.  It looks to me like your "getLastModified()" method is returning the last time 
this very jsp was edited.  To me that seems misleading:  suppose a developer 
mistakenly uses your feature, but their jsp is actually quite dynamic and its response 
changes with each new request.  They are going to have a helluva time figuring out why 
their jsp refuses to change its output.  I could be mistaken in my understanding of 
what's going on, though.

Instead I think your getLastModified() method should look like this instead:

protected long getLastModified(HttpServletRequest request)
{
        return -1;
}

Notice from Javadoc:
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServlet.html#getLastModified(javax.servlet.http.HttpServletRequest)

Returns:
a long integer specifying the time the HttpServletRequest object was last modified, in 
milliseconds since midnight, January 1, 1970 GMT, or -1 if the time is not known

Now you'll have to add a little bit of code to deal with the "-1" circumstance in your 
service() method.  Thanks for your post though - very interesting.  


Julius Davies, Programmer, CUCBC
Email: [EMAIL PROTECTED], Ph: 604.730.6385


> -----Original Message-----
> From: Noel J. Bergman [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, December 03, 2002 2:25 PM
> To: Tomcat User List
> Subject: JSP Last-modified/If-modified-since
> 
> 
> This seems to be an FAQ item without a good solution, at 
> least that I've
> noticed on the list.  Recently I had to address this myself, 
> moving some
> sites over to Tomcat.  Here is my solution, for critique.
> 
> First, I subclassed Jasper's HttpJspBase to add the desired behavior:
> 
> abstract public class LastModifiedJSP extends
> org.apache.jasper.runtime.HttpJspBase
> {
>       protected long getLastModified(HttpServletRequest request)
>       {
>               long lm = (new
> java.io.File(getServletConfig().getServletContext().getRealPat
> h(request.getS
> ervletPath())).lastModified()) / 1000L * 1000L;
>               return lm;
>       }
> 
>       public void service(ServletRequest req, ServletResponse res)
>                       throws ServletException, IOException
>       {
>               HttpServletRequest request = (HttpServletRequest) req;
>               HttpServletResponse response = 
> (HttpServletResponse) res;
> 
>               long checkDate = 
> request.getDateHeader("If-Modified-Since");
>               long lastMod = getLastModified(request);
>               if (checkDate > 0 && lastMod <= checkDate)
>               {
>                       
> response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
>                       return;
>               }
>               response.setDateHeader("Last-Modified", lastMod);
> 
>               super.service(request, response);
>       }
> 
>       public String getServletInfo()
>       {
>               return "Base class for JSP pages that want 
> Conditional Get handling.
> Default getLastModified() returns lastModified() for JSP 
> source file.";
>       }
> }
> 
> then each page that wants to use this behavior includes:
> 
>    <%@ page extends="com.devtech.jsp.LastModifiedJSP" %>
> 
> That's it.  If a page wants to change its Last-Modified 
> behavior, it can
> optionally provide a getLastModified() method, just as a 
> servlet would do.
> And I will change the default behavior to also check included 
> pages via
> getIncludes().
> 
>       --- Noel
> 
> 
> --
> To unsubscribe, e-mail:   
> <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: 
> <mailto:[EMAIL PROTECTED]>
> 
> 

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

Reply via email to