Skip to main content

skip to main content

developerWorks  >  Java technology  >

JSP best practices: The power of time stamps

Time-stamp your JSP pages and personalize your Web site

developerWorks
Document options

Document options requiring JavaScript are not displayed


Rate this page

Help us improve this content


Level: Introductory

Brett McLaughlin (brett@oreilly.com), Author, O'Reilly and Associates

01 Jul 2003

Brett McLaughlin continues his JSP best practices series with a look at the various techniques for adding time stamps to your JSP pages, as well as the ways that this simple modification can enhance the usability of your Web site.

Time stamps are typically used on simple Web sites, like personal sites or content-driven ones, letting users see when the page was last updated. As a result, it is easy to overlook their essential usefulness, particularly for e-commerce sites. In this tip, I'll show you how adding time stamps to your JSP pages can lead to a more easeful experience for users, as well as opening the door to personalization on your site.

Adding a time stamp

Placing a last-modified date or time stamp on a JSP page is no different from placing it on any other type of page. The java.io.File class offers the lastModified method for just this purpose. The only tricky part is getting a File handle to a JSP page deployed in a Web container. Listing 1 shows a short piece of code (a scriptlet when placed within a JSP page) that will get you over the hump.

Note that both application and request are built-in variables that every JSP page can access. application represents the instance of the class that implements the javax.servlet.ServletContext object for this JSP page. That object, in turn, is able to get the servlet path of the JSP page, which turns into a physical path for your JSP page. This path can then be turned into a File.


Listing 1. Getting a file handle to a JSP
String jspPath = application.getRealPath(request.getServletPath());
java.io.File jspFile = new java.io.File(jspPath);

To take all this information and put it into action, use a code snippet like the one shown in Listing 2:


Listing 2. Printing out a time stamp
<%
     String jspPath = application.getRealPath(request.getServletPath());
     java.io.File jspFile = new java.io.File(jspPath);
     out.println(new java.util.Date(jspFile.lastModified()));
%>

After the file's last-modification date is determined, it is turned into a Date object, which is directly output to the screen. Pretty basic code for a rather nice result.



Back to top


Reusable time stamps

Of course, it would be a pain (and also redundant) to add a code snippet like the one above for every page you wanted to time-stamp. A better approach is to create a generic JSP page that displays the date and time stamp, and then include that JSP page in all the locations you want to time-stamp. For this, you simply save Listing 2 as timestamp.jsp, then use the jsp:include mechanism to output the time stamp wherever you need it. Listing 3 shows a simple footer page that includes a JSP time stamp:


Listing 3. Including the time stamp in other JSP pages
<!-- Begin footer section -->
     <tr>
       <td width="91" align="left" valign="top" bgcolor="#330066"> </td>
       <td align="left" valign="top"> </td>
       <td class="footer" align="left" valign="top">
           <div align="center"><br>
           &copy; 2003 <a href="mailto:webmaster@newInstance.com">Brett 
               McLaughlin</a><br>
       Last Modified: <jsp:include page="timestamp.jsp" flush="true" />
         </div></td>
           <td align="left" valign="top"> </td>
       <td width="141" align="right" valign="top" bgcolor="#330066"> </td>
     </tr>
</table>
<!-- End footer section -->

You've probably noted that the file obtained in Listing 3 represents the parent JSP page (in this case footer.jsp), not the included time-stamp file. So, the last-modification date and time stamp is pulled from footer.jsp, not from timestamp.jsp. This is important because your time-stamp file is actually a static file; it doesn't change. The last-modification query should, therefore, go to an actively updated page such as footer.jsp.

To take this one step further, consider a site that consists of various content pages. Each of these pages includes a single footer.jsp page, which in turn includes timestamp.jsp. If the last-modification date queries the top-most page (which in this case would be the content page), the time stamp will only change as content is modified, which is exactly the desired effect.



Back to top


Formatting your time stamp

The default date and time stamp output is rather unattractive: Fri Mar 28 10:30:10 CST 2003. Fortunately, you can use the java.text.SimpleDateFormat class to better control the format of your output. Listing 4 shows timestamp.jsp with the addition of java.text.SimpleDateFormat.


Listing 4. Formatting your time stamp
<%
     String jspPath = 
       application.getRealPath(request.getServletPath());
     java.io.File jspFile = new java.io.File(jspPath);
     java.util.Date lastModified = 
       new java.util.Date(jspFile.lastModified());
     java.text.SimpleDateFormat fmt =
       new java.text.SimpleDateFormat("MMM dd, yyyy, K:mm a (zz)");
     out.println(fmt.format(lastModified));
%>

The resulting output is Mar 28, 2003, 10:30 AM (CST), a much nicer looking date and time stamp for users. Consult the Javadoc for the java.text package to see other options for formatting.



Back to top


Working with WARs

When you deploy your JSP pages as part of a Web application within a WAR (Web archive) file you will run into a slight limitation, which fortunately has a fairly simple workaround. Servlet containers usually deploy WAR files by expanding them into a temporary directory, and then serving content from that temporary directory. The trouble is, in these circumstances you will not be able to obtain the File object associated with the JSP page. Rather, you will only be able to create a handle to the temporary file, which may be recreated every time the Web container restarts. If left untended the last-modified date will also change on every restart.

The workaround is to manually extract the WAR file on the server on which it is to be deployed. The inconvenience of this manual extraction is relatively minor (especially since you can use the zip tool to extract WAR files) and once you've done it your time stamps will work as described here.



Back to top


Conclusion

In addition to reassuring users that the content on your site is fresh and timely, adding time and date stamps is a first step to personalizing your Web site. After you have added a time stamp to a Web page (or JSP page), it's trivial to script a program to e-mail users as selected pages are updated.

In the next installment, we'll continue to play around with time stamps, incorporating the use of custom tag libraries. Until then, I urge you to try out the techniques you've learned here, and I'll see you online.



Resources



About the author

Photo of Brett McLaughlin

Brett McLaughlin has been working in computers since the Logo days (remember the little triangle?). He currently specializes in building application infrastructure using Java and Java-related technologies. He has spent the last several years implementing these infrastructures at Nextel Communications and Allegiance Telecom Inc. Brett is one of the co-founders of the Java Apache project Turbine, which builds a reusable component architecture for Web application development using Java servlets. He is also a contributor of the EJBoss project, an open source EJB application server, and Cocoon, an open source XML Web-publishing engine. Contact Brett at brett@oreilly.com.




Rate this page


Please take a moment to complete this form to help us better serve you.



YesNoDon't know
 


 


12345
Not
useful
Extremely
useful
 


Back to top