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

JSTL - Syntax and Expression Language

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...)

Do you have any supprises when you compare your guess with output? I have some explanations on the output to help you:

  • Example 3 shows you that the result of integer operations is not converted by to integer.
  • Example 5 confirms that null is converted to false.
  • Example 7 shows ycu that value will be converted back to an identifier (variable name).
  • Examples 10 and 12 show you two correct ways of accessing object properties.
  • Examples 11 is understandable, because an expression is expected in side "[]" and "method" without quotes is an identifier and it matches no existing objects (variable names).
  • Examples 13 and 14 have syntax errors, because the "." operator only takes identifiers (names) as operands.
  • Exampels 15 and 16 are different ways of writting nested "." operations.
  • Exampels 24 and 25 have syntax errors, because "0" is not allowed an identifier.
  • Exampels 30 and 31 tell us that variables declared by scriptlet are not available to JSTL.
  • Exampel 32 is working because Java offers Object.getClass().getName().
  • Exampel 33 is not working because "[name]" evaluates to null.
  • Exampels 34 and 35 show you that the out is not directly accessible, but it is an accessible property of pageContext.
  • Exampels 36 and 37 show you that new elements can be added to implicit map object, sessionScope.

Exercise: Write an example JSP page to show how to create a JavaBean object, and how to access the properties of this object with JSTL expressions.

pageContext Attributes and JSTL Top Level Identifiers

Based the rules and the examples in the previous sections, we can easily conclude that:

  • JSTL top level identifiers (variables) are pageContext attributes.
  • pageContext attributes are JSTL top level identifiers (variables).

Here is a sample code to show you that you can mix variables and pageContext attributes any way you want:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" 
   xmlns:c="http://java.sun.com/jstl/core" version="1.2"> 
<!-- ExpVariable.jsp
     Copyright (c) 2003 by Dr. Herong Yang
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<c:set var="message" value="Hi there!"/>
<c:out value="1. ${message}"/><br/>
2. <jsp:expression>pageContext.findAttribute("message")
   </jsp:expression><br/>

<jsp:scriptlet><![CDATA[
   String s = "Hello world!";
   pageContext.setAttribute("hello", s, PageContext.PAGE_SCOPE);
]]></jsp:scriptlet>
<c:out value="3. ${hello}"/><br/>

<jsp:useBean id="today" class="java.util.Date"/>
<c:out value="4. ${today}"/><br/>

<jsp:scriptlet><![CDATA[
   java.util.Date d = new java.util.Date();
   d.setTime(d.getTime()+24*60*60*1000);
   pageContext.setAttribute("tomorrow", d, PageContext.PAGE_SCOPE);
]]></jsp:scriptlet>
<c:out value="5. ${tomorrow}"/><br/>

<jsp:setProperty name="today" property="time" value="1000000000000"/>
<c:out value="6. ${today}"/><br/>
<c:out value="7. ${today.time}"/><br/>

</body></html>
</jsp:root>

Here is the output this page:

1. Hi there!
2. Hi there!
3. Hello world!
4. Sat Jul 12 14:06:16 EDT 2003
5. Sun Jul 13 14:06:16 EDT 2003
6. Sat Sep 08 21:46:40 EDT 2001
7. 1000000000000

Note that:

  • useBean element also adds an attribute to pageContext. For more information see chapter "Using JavaBean Classes".

Exercise: Since there many methods to add an attribute to pageContext, useBean, c:set, and pageContext.setAttribute, write a simple program to show what happens if the same attribute name is used by different methods.

Part:   1  2  3  4 

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