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

The literal data rules are easy to understand, with a couple of exceptions:

  • No literal data for character data type.
  • String can be quoted by single quote (').

Named variables are coming from two sources:

  • Variables defined by the JSTL "set" action.
  • Variables provided as attributes in the pageContext object prepared by other server side codes, like useBean and scriptlet JSP elements.

Variables provided as pageContext attributes will have their original Java types, like int, float, char, or Object. But all JSTL operations will be carried out in one of the 5 types of literal data. Operators of other types will be converted before the operation.

Type conversion rules:

  • To string - Using the Java rules, except that null will be converted to "".
  • To boolean - Using the Java rules, except that null or "" will be converted to false.
  • To integer - Using the Java rules, except that null or "" will be converted to 0.
  • To floating porint number - Using the Java rules, except that null or "" will be converted to 0.0.
  • To object - Using the Java rules, except that "" will be converted to null.

Basic Operators and Operations

JSTL supports all logical, relational and arithmetic operators supported in Java, with exceptions:

  • "eq", "ne", "lt", "gt", "le", and "ge" could be used as relational operators.
  • "and", "or" and "not" could be used as logical operators.

Implicit Objects

JSTL has a set of predefined objects accessible by the following variable names:

  • pageContext - the PageContext object.
  • pageScope - a Map that maps page-scoped attribute names to their values.
  • requestScope - a Map that maps request-scoped attribute names to their values.
  • sessionScope - a Map that maps session-scoped attribute names to their values.
  • applicationScope - a Map that maps application-scoped attribute names to their values.
  • param - a Map that maps parameter names to a single String parameter value (obtained by calling ServletRequest.getParameter(String)).
  • paramValues - a Map that maps parameter names to a String[ ] of all values for that parameter (obtained by calling ServletRequest.getParameterValues(String)).
  • header - a Map that maps header names to a single String header value (obtained by calling ServletRequest.getheader(String)).
  • headerValues - a Map that maps header names to a String[ ] of all values for that parameter (obtained by calling ServletRequest.getHeaders(String)).
  • cookie - a Map that maps cookie names to a single Cookie (obtained by calling HttpServletRequest.getCookie(String)).
  • initParam - a Map that maps a parameter names to a single String parameter value (obtained by calling ServletRequest.getInitParameter(String)).

Accessing Collection Elements and Object Properties

JSTL supports reference operations to collection elements and object properties by using "." and "[]" operators. But they are used in the way as ECMAScript, which is very different than Java. Here is the main steps of the evaluation process:

  • 0. Check syntax to only allow: "identifier_a.identifier_b" and "identifier_a[expression_b]".
  • 1. Replacing "." operator by "[]" operator. So convert "identifier_a.identifier_b" to "identifier_a.['identifier_b']".
  • 2. Evaluate the second operand. So convert "identifier_a[expresion_b]" to "identifier_a[value_b]".
  • 3. If the first operand matches no existing object name, return null.
  • 4. If the second operand evaluates to null, return null.
  • 5. If the first operand reprents an array, try to finish the operation as "[]". So try to evaluate "identifier_a[value_b]" as is.
  • 6. If the first operand reprents a map, try to finish the operation with a call to get(). So try to evaluate "identifier_a[value_b]" as "identifier_a.get(value_b)".
  • 7. If the first operand represents an object of orther types, try to finish the operation as a Java bean property accessing operation. So convert "identifier_a[value_b]" to "identifier_a[identifier_b]", then evaluate it as "identifier_a.get'Identifier_b'()".
  • 8. If step 7 failed, try to finish the operation as an object member variable. So evaluate "identifier_a[identifier_b]" as "identifier_a.identifier_b". I am note so sure about this. Needs further research.

(Continued on next part...)

Part:   1  2  3  4 

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