"include" Directive and Action Elements

This section provides a tutorial example on how to 'declaration' element, 'include' directive element and 'include' action elements.

In order to practice how to write JSP Documents (JSP pages in XML format), let's look a more complex tutorial example. It 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 Document, CurrentTime.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- CurrentTime.jsp
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<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)+"<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.jspx"/>
</p>
<p>
<jsp:include page="TimeStamp.jspx"/>
</p>
</body></html>
</jsp:root>

Here is the JSP Document used by the "include" directive element, JvmStamp.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- JvmStamp.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<b>Current JVM:</b><br/> 
<jsp:scriptlet>
   String s;
   s = "java.vm.name";
   out.println(s+": "+System.getProperty(s)+"<br/>");
   s = "java.vm.version";
   out.println(s+": "+System.getProperty(s)+"<br/>");
   s = "os.name";
   out.println(s+": "+System.getProperty(s)+"<br/>");
   s = "os.version";
   out.println(s+": "+System.getProperty(s)+"<br/>");
</jsp:scriptlet>
</jsp:root>

Here is the JSP Document used by the "include" action element, TimeStamp.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- TimeStamp.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<b>Current Time: </b>
<jsp:scriptlet>
   java.util.Date d = new java.util.Date();
   out.println(d.toString()); 
</jsp:scriptlet>
</jsp:root>

Couple of interesting things to remember in this example:

Deploy all 3 JSP Documents to the Tomcat 7 server and try to answer following questions:

See next section for my answers.

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

Syntax of JSP Pages and JSP Documents

 Syntactic Elements of a JSP Page

 JSP Document - JSP Page in XML Format

 Writing Scriptlet Element in XML Format

 Writing Directive Element in XML Format

 Writing Action Element in XML Format

"include" Directive and Action Elements

 Execution Result of CurrentTime.jspx

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version