package com.acme.tag; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import javax.servlet.http.*; import java.util.*; import java.net.*; /** * A Tag for checking last modification date of the current JSP * @author Magnus Rydin */ public class LastModifiedTag extends TagSupport { private Locale locale; private HttpServletRequest request; /** * Constructor */ public LastModifiedTag() { super(); } /** * Method used by the JSP container to set the Locale to use. */ public void setLocale(Locale locale) { this.locale=locale; } /** * Method Called at end of Tag * @return either EVAL_PAGE or SKIP_PAGE */ public int doEndTag() throws javax.servlet.jsp.JspTagException { try{ //get the request request= (HttpServletRequest) pageContext.getRequest(); if(locale==null){ //if no Locale was given, use the Locale set by the user locale=request.getLocale(); } if(locale==null){ //if no Locale was set by the user, use system default locale=Locale.getDefault(); } //obtain the resource from the ServletContext URL url=pageContext.getServletContext().getResource(request.getServletPath()); //read the lastModified value. Need to check for file due to a bugg in Suns VM long miliseconds; if(url.getProtocol().equals("file")){ miliseconds=new java.io.File(url.getFile()).lastModified() ; }else{ miliseconds=url.openConnection().getLastModified(); } //create a date out of the long if greater than 0 if(miliseconds>0){ //long miliseconds=(url.getProtocol().equals("file") ? new java.io.File(url.getFile()).lastModified() : url.openConnection().getLastModified()); Date lastModified=new Date(miliseconds); //write the date using the Locale to format. pageContext.getOut().write(java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.SHORT, java.text.DateFormat.SHORT,locale).format(lastModified)); } }catch(java.net.MalformedURLException mue){ throw new JspTagException(mue.getMessage()); }catch(java.io.IOException ioe){ throw new JspTagException(ioe.getMessage()); }catch(java.lang.NullPointerException npe){ //do nothing } return EVAL_PAGE; } }