JSTL Quick Reference
Copyright 2003 Bill Siggelkow
Expressions (EL)
In Attribute Values
•
•
Bean Property Access
bean.name
bean["name"]
Indexed Property Access
bean.property[index]
Map Property Access
bean.property["key"]
Implicit Objects (maps)
Object Description
pageContext JSP Page Context object
pageScope Page-scoped variables (valid only on a
given JSP page)
requestScope Request-scoped variables (valid for a given
request)
sessionScope Session-scoped variables (valid for the
user's session)
applicationScope Application-scoped variables (valid for a
given application context)
param Map of request parameter name to a String
parameter value
paramValues Map of request parameter name to a String
array of parameter values
header Map of request header name to a header
String value
Object Description
headerValues Map of request header name to a String
array of values
cookie Map of cookie name to a Cookie object
initParam Map of context initialization parameter
name to a String parameter value (set in
web.xml)
Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ (div) Division
% (mod) Remainder (modulus)
Relational Operators
Operator Description
== (eq) Equality
!= (ne) Inequality
< (lt) Less than
> (gt) Greater than
<= (le) Less than or equal to
>= (ge) Greater than or equal to
<= (le) Less than or equal to
Logical Operators
Operator Description
&& (and) True if both operands are true; false,
otherwise.
|| (or) True if either or both operands are true; false,
otherwise.
Operator Description
! (not) True if operand is false; false, otherwise.
Other Operators
Operator Description
empty True if the operand is null, an empty String,
empty array, empty Map, or empty List; false,
otherwise.
() Paranthesis for changing operator
precedence.
EL Functions (JSTL 1.1)
Note: All functions treat null Strings as empty Strings.
<%@ taglib prefix="fn"
uri="http://java.sun.com/jstl/functions" %>
Usage: ${fn:function(arg0, ...)}
We offer ${fn:length(flavorSet)} ice cream
flavors.
Function Description
fn:contains
(string, substring) :
boolean
Returns true if substring is contained
in string; false, otherwise.
fn:containsIgnoreCase
(string, substring) :
boolean
Returns true if substring is contained
in string regardless of case; false,
otherwise.
fn:endsWith
(string, suffix) :
boolean
Returns true if string ends with the
specified suffix; false, otherwise.
fn:escapeXml
(string) : String
Escapes characters (e.g changing “<”
to “<”) that could be interpreted as
XML (including HTML) markup.
fn:indexOf
(string, substring) : int
Returns an integer representing the 0-
based index within string of the first
occurrence of substring. If substring is
empty, 0 is returned.
Function Description
fn:join
(string[], separator) :
String
Joins all elements of the string array
into a single string. Separator
separates each element in the
resulting string. If separator is an
empty string, the elements are joined
without a separator.
fn:length
(collection or string) :
int
If a collection or array is passed, the
size of the collection or array is
returned; If a string is passed, the
number of characters in the string is
returned.
fn:replace
(inputString,
beforeSubstring,
afterSubstring) : String
Replaces in inputString, every
occurrence of beforeString with
afterString. An empty string is
returned if either inputString or
beforeString is empty. If afterString is
empty, all occurences of the
beforeString are removed.
fn:split
(string, delimiters) :
String[]
Splits string into a string array using
the given set of delimiter characters.
The delimiter characters are not
included in any returned tokens.
fn:startsWith
(string, prefix) :
boolean
Returns true if string starts with the
specified prefix; false, otherwise.
Returns true if prefix is empty.
fn:substring
(string, beginIndex,
endIndex) : String
Returns a subset of string using the
zero-based indices – inclusive of the
begin index, but exclusive of the end
index.
fn:substringAfter
(string, substring) :
String
Returns the subset of string following
the given substring.
fn:substringBefore
(string, substring) :
String
Returns the subset of string that
procedes the given substring.
fn:toLowerCase
(string) : String
Converts all characters of a string to
lowercase.
fn:toUpperCase
(string) : String
Converts all characters of a string to
uppercase.
fn:trim
(string) : String
Removes whitespace from both ends
of a string.
Core Tag Library
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
General-Purpose Actions
Actions for rendering data, creating and
modifying scoped variables, and catching
exceptions.
- renders data to the page
Welcome,
Attribute Description Rqd Default
value Data to output Yes None
default Fallback data to output if
value is empty
No Body
escapeXml true to escape special
characters
No true
- saves data to a scoped variable
You are in dog
years.
Attribute Description Rqd Default
value Data to save No Body
target Name of variable to modify No None
property Property of target to modify No None
var Name of variable to store
data
No None
scope Scope of variable No page
- deletes a scoped variable
Attribute Description Rqd Default
var Name of variable to delete Yes None
scope Scope of variable No All
scopes
- traps all exceptions or errors from the
enclosed body.
Could not connect to Java web site.
Attribute Description Rqd Default
var Name of variable to hold the
thrown exception, if any.
Variable will be of type
java.lang.Throwable.
No None
Conditional Actions
Actions for processing markup based on logical
conditions.
- processes the body if test is true
You are over the hill.
Attribute Description Rqd Default
test Condition to evaluate Yes None
var Name of variable to store
test condition's result
No None
scope Scope of variable No page
- multiple conditions – processes the
body of the first enclosed when tag where the test
condition is true. If none match then the body of the
otherwise tag (if present) is processed.
// do something
// do something else
// do this when nothing else is true
The choose tag accepts no attributes and can only
contain when tag(s) and an optional otherwise tag.
- processes the body if test is true and no
other previous tags evaluated to true.
Attribute Description Rqd Default
test Condition to evaluate Yes None
- processes the body if no other
previous condition matched. This tag
accepts no attributes and, if present, must be the last
tag in the body.
Iterator Actions
Actions that loop over collections, for a fixed number
of times, or over a set of string tokens. These
actions share the following attributes for iterating over
a subset of elements.
Attribute Description Rqd Default
begin Zero-based index of first item
to process, inclusive.
No 0
end Zero-based index of last item
to process, inclusive.
No Last item
step Process every stepth element
(e.g 2 = every second
element).
No 1
Attribute Description Rqd Default
varStatus Name of variable to hold the
loop status with the following
properties:
• index – position of the
current item
• count – number of times
through the loop (starting
with 1)
• first – boolean
indicator if this is the first
iteration
• last – boolean indicator
if this is the last iteration
No None
- repeats the nested body content
over a collection or for a fixed number of times.
You speak these languages:
Attribute Description Rqd Default
var Name of variable to hold
the current item. This
variable has only nested
visibility.
No None
items Collection, iterator, map, or
array to loop over.
No None
- repeats the nested body
content for each token of a delimited string.
Fred,Joe,Mary
Attribute Description Rqd Default
Attribute Description Rqd Default
var Name of variable to hold
the current token. This
variable has only nested
visibility.
No None
items String of tokens to loop
over.
Yes None
delims Set of characters that
separate the tokens (e.g.
delims=”,;” will tokenize a
string separated by
commas or semi-colons).
Yes None
URL Related Actions
Actions for importing content from URLs, building
URLs, and redirecting.
- imports the content of a URL-based
resource. Action may include nested
tags to specify the query string (unless the
varReader attribute is specified).
Hello World
Attribute Description Rqd Default
url URL of the resource to
import.
Yes None
context Name of the context
(beginning with a /) of some
other local web application to
import the resource from.
No Current
context
var Name of the variable to hold
the imported content as a
String.
No None
scope Scope of the var variable No page
varReader Name of the variable to hold
the imported content as a
Reader. This variable has
only nested visibility so that
the reader will always be
closed.
No None
- builds a URL with the proper rewriting rules
applied (only relative URLs are rewritten). Action
may include nested tags to specify
the query string.
Edit Profile
Attribute Description Rqd Default
value URL to be processed. Yes None
context Name of the context
(beginning with a /) of some
other local web application.
No Current
context
var Name of the variable to hold
the URL as a String.
No None
scope Scope of the var variable No page
- sends the client a response to
redirect to the specified URL. This action will
abort processing of the current page. Action may
include nested tags to specify the
query string.
Attribute Description Rqd Default
url URL of the resource to
redirect to.
Yes None
context Name of the context
(beginning with a /) of some
other local web application.
No Current
context
- adds request parameters to a URL.
This action can only be nested within
, , or
.
Attribute Description Rqd Default
name Name of the query string
parameter.
Yes None
value Value of the parameter. If not
specified, value is taken from
the tag body.
No Body
Formatting Tag Library
<%@ taglib prefix="fmt"
uri="http://java.sun.com/jstl/fmt" %>
Internationalization (I18N) Actions
Actions that establish localization (l10n) contexts,
specify resource bundles, and format messages.
- Sets the default locale for the
specified scope. This will override the browser-
based locale.
Attribute Description Rqd Default
value String representation of a
locale (e.g. en_US) or an
actual java.util.Locale object.
Yes None
variant Locale variant (as a String) to
specify in conjunction with the
locale (language and country).
No None
scope Scope to set the default locale
for.
No page
- Sets the localization context, based
on the specified resource bundle, to be used within
the body content of this tag.
Attribute Description Rqd Default
basename Fully-qualified name of the
base bundle without a file type
(such as “.properties”).
Yes None
prefix String prefix to be prepended
to the value of the message
key. Note that the prefix
must include all characters –
a separator character (e.g. “.”)
is not assumed.
No None
- Creates and stores in a scoped
variable, a localization context based on the specified
resource bundle.
Attribute Description Rqd Default
basename Fully-qualified name of the
base bundle without a file type
(such as “.properties”).
Yes None
var Name of the variable to hold
the localization context.
No Default
l10n
context
(see
“Configu
ration”)
scope Scope of the var variable No page
- Looks up a localized message in a
resource bundle. This tag can contain nested
tags to specify message format
substitution values. The resultant message is printed
or stored in a scoped variable.
Attribute Description Rqd Default
key Message key to be looked up. No Body
bundle Localization context (set by prio
configuration, , or
, which specifies
the resource bundle the message
key is to be looked up in.
No Default
l10n
context
var Variable to hold the message. No
scope Scope of the var variable. No page
- Supplies a parameter for message
format substitution in a containing
tag. Parameters are substituted in sequential order.
Attribute Description Rqd Default
value Value used for parametric
message format substitution.
No Body
- Instructs JSTL to use
a specific character encoding (see
http://www.iana.org/assignments/character-sets)
to decode request parameters. Omitting a value
indicates to use automatic detection of the proper
encoding.
Attribute Description Rqd Default
value Character encoding (e.g. “UTF-
8”) to use.
No Automatic
Formatting Actions
Actions that format and parse numbers,
currencies, percentages, dates and times.
- Sets the specified time zone
to be applied to the nested body content. The
following example demonstrates that the time
zone by this action has only nested visibility.
Pacific Time:
Local Time:
Attribute Description Rqd Default
value String representation of a time
zone (such as
“America/New_York”, “GMT-5”,
or “EST”) or an actual
java.util.TimeZone object.
Yes None
- Sets the specified time zone
in a named scoped variable or using the default time
zone name if var is not specified.
Mountain Time:
Attribute Description Rq
d
Default
value String representation of a time
zone (such as
“America/New_York”, “GMT-
5”, or “EST”) or an actual
java.util.TimeZone object.
Yes None
var Name of the variable to store
the time zone.
No Default
time zone
(see
“Configur
ation”)
scope Scope of the var variable No page
- Formats a number,
currency, or percentage in a locale-sensitive manner.
The formatted value is printed or stored in a scoped
variable.
Attribute Description Rqd Default
value Numeric value to
format.
No Body
type Specifies the type of
value. Valid values are:
• number
• currency
• percentage
No number
pattern Custom formatting
pattern (overrides other
formatting options
including type – see
java.text.DecimalFormat
)
No None
Attribute Description Rqd Default
currencyCode Currency code (ISO
4217) used for
formatting currencies.
Such as “USD” (US
dollars) or “EUR” (euro).
No Based
on
default
locale
currencySymbol Currency symbol used
when formatting
currencies. Such as
“$” for US dollars, or “F”
for Francs.
No Based
on
default
locale
groupingUsed Specifies if grouping
separators will be used
(for example –
formatting “23890” as
“23,890”).
No true
maxIntegerDigits Maximum number of
digits to print in the
integer part of the
number.
No None
minIntegerDigits Minimum number of
digits to print in the
integer part of the
number.
No None
maxFractionDigit
s
Maximum number of
digits to print in the
fractional part of the
number.
No None
minFractionDigit
s
Minimum number of
digits to print in the
fractional part of the
number.
No None
var Variable to store the
formatted number.
No None
scope Scope of the var
variable
No page
- Parses a String representing
a number, currency, or percentage in a locale-
sensitive manner. The parsed value is printed or
stored in a scoped variable.
Attribute Description Rqd Default
value Value to parse. No Body
type Specifies the type of
value. Valid values are:
• number
• currency
• percentage
No number
pattern Custom parsing pattern
(overrides type – see
java.text.DecimalFormat)
No None
parseLocale String representation of a
locale (e.g. en_US) or an
actual java.util.Locale
object used for parsing.
No Default
locale
integerOnly Specifies if only the
integer portion of the
value should be parsed.
No false
var Variable to store the
formatted number.
No None
scope Scope of the var variable No page
Formatting Dates
Dates are formatted and parsed using the
and
actions which share the following common
attributes.
Attribute Description Rqd Default
type Specifies the type of value. Valid
values are:
• time (time only)
• date (date only)
• both (date and time)
No date
dateStyle Predefined formatting style for a
date (ignored if type=”time”) -- see
java.text.DateFormat.
Valid values are:
• default (Jul 19, 2003)
• short (7/19/03)
• medium (Jul 19, 2003)
• long (July 19, 2003)
• full (Saturday, July 19,
2003)
No default
Attribute Description Rqd Default
timeStyle Predefined formatting style for a
time (ignored if type=”date”) -- see
java.text.DateFormat.
Valid values are:
• default (2:51:16 PM)
• short (2:51 PM)
• medium (2:5