You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Noel J. Bergman" <no...@devtech.com> on 2002/12/03 23:24:30 UTC

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().getRealPath(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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>