JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

JSP Elements

Part:   1  2  3  4 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

JSP Example - "CurrentTime.jsp"

The following example has three JSP files working together to show you how to use "decalaration" elements, "include" directive elements and "include" action elements. Here is the main JSP file, CurrentTime.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- CurrentTime.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:directive.page import="java.util.*"/>
<jsp:directive.page import="java.text.*"/>
<jsp:declaration>
   private Date now;
   private JspWriter out;
   private void printTime(String tz) throws Throwable {
      DateFormat df = DateFormat.getInstance();
      df.setTimeZone(TimeZone.getTimeZone(tz));
      out.println(tz+": "+df.format(now)+"&lt;br/>"); 
   }
</jsp:declaration>
<p>
<b>Current time in different time zones:</b><br/>
<jsp:scriptlet>
   this.out = out;
   now = new Date();
   printTime("America/New_York");
   printTime("America/Los_Angeles");
   printTime("Asia/Shanghai");
   printTime("Europe/Paris");
   printTime("Europe/Moscow");
</jsp:scriptlet>
</p>
<p>
<jsp:directive.include file="JvmStamp.jsp"/>
</p>
<p>
<jsp:include page="TimeStamp.jsp"/>
</p>
</body></html>
</jsp:root>

Here is the JSP file used by the include directive element, JvmStamp.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- JvmStamp.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
<b>Current JVM:</b><br/> 
<jsp:scriptlet>
   String s;
   s = "java.vm.name";
   out.println(s+": "+System.getProperty(s)+"&lt;br/>");
   s = "java.vm.version";
   out.println(s+": "+System.getProperty(s)+"&lt;br/>");
   s = "os.name";
   out.println(s+": "+System.getProperty(s)+"&lt;br/>");
   s = "os.version";
   out.println(s+": "+System.getProperty(s)+"&lt;br/>");
</jsp:scriptlet>
</jsp:root>

Here is the JSP file used by the include action element, TimeStamp.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- TimeStamp.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
<b>Current Time: </b>
<jsp:scriptlet>
   java.util.Date d = new java.util.Date();
   out.println(d.toString()); 
</jsp:scriptlet>
</jsp:root>

Here is what I got by requesting CurrentTime.jsp from IE:

Current time in different time zones:
America/New_York: 12/23/02 9:38 PM
America/Los_Angeles: 12/23/02 6:38 PM
Asia/Shanghai: 12/24/02 10:38 AM
Europe/Paris: 12/24/02 3:38 AM
Europe/Moscow: 12/24/02 5:38 AM

Current JVM:
java.vm.name: Java HotSpot(TM) Client VM
java.vm.version: 1.3.1_01
os.name: Windows 2000
os.version: 5.0

Current Time: Mon Dec 23 21:38:10 EST 2002

(Continued on next part...)

Part:   1  2  3  4 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - JSP Elements