The JavaServer Pages Standard Tag Library (JSTL) - JSP

So far in this chapter, JavaServer Pages have been introduced as consisting of directive, declaration, expression, and scriptlet tags. Although the scriptlet tag does afford you a great deal of control over the dynamic aspects of a page, the downside is that you end up with HTML and Java on the same page. In practice, this becomes very difficult to manage in short order.

Introducing the JSTL

As mentioned earlier in this chapter (in the “Basic Introduction to the taglib Directive” section), it is possible to write your own tags for use within JSP. For wellknown tasks, such as conditional processing and iteration, it makes sense to have tested and reliable implementations of custom tags.This is where the JavaServer Pages Standard Tag Library (JSTL) comes in.

There are various tag library implementations available, but the examples in this chapter use the JSTL reference implementation from the Jakarta-Taglibs project at

The JavaServer Pages Standard Tag Library (JSTL) 83

Consider the times that you want to iterate over the contents of a collection, or make a decision over what to embed into a page depending on the value of a variable.You saw an example of looping over the contents of an array in Listing duplicated here for convenience, in Listing

Listing 2.29 Iteration Using a Scriptlet

The following lines contain a mixture of Java code and HTML, which is difficult to read and therefore difficult to maintain:

It also means that whoever is editing the page needs to be comfortable with both languages.

It is for situations such as this that the JavaServer Pages Standard Tag Library (JSTL) was designed. Rather than the page developer using scriptlets or third-party tags within a page, the JSTL defines a set of standard tags that can be used instead.The benefit here is that not only do you not have to use Java code in a JSP, but you only have to learn the replacement tag once and you can use it in many JSP containers.The JSP container implementations can of course implement the tags as they see fit, so different containers might perform better than others in different situations.

Using the JSTL

The example in Listing can be rewritten so that it does not use a scriptlet, by using tags from the JSTL . Several parts of the listing are new.

Listing 2.30 JSTL Iteration Example

In this example, the first two lines are new and import tags, which are used later on, from the JSTL. Everything else is much the same, except the scriptlet has been replaced, and the following lines now use standard tags for iteration and output:

The forEach tag enables you to iterate over a collection of objects, and the out tag inserts text into the output stream. The JSTL provides four groups of tags:

  • Core tags for flow control, expressions, and URL management
  • SQL tags for SQL database access
  • Internationalization tags for formatting and parsing text
  • XML tags for parsing, searching, and transforming XML documents

JSTL 1.0 provided two versions of the tag libraries: one that supported the Expression Language, and one that did not.The latter form supported request-time expressions and was provided so that you can use the standard actions without having to use the new Expression Language. Since JSP 2.0 contains the Expression Language, there is just one library in JSTL 1.1 that accepts both forms of expression.

The JSP 2.0 Expression Language

JSP 2.0 introduces a new Expression Language.The rationale for this is that at the moment, whenever a page author needs to access a value of a JavaBean, he must use an expression tag of the form:

<%= someBean.getSomeValue() %>

When this syntax is used as an attribute value to any kind of tag, the result quickly becomes difficult to read:

<somePrefix:someCustomTag someAttribute=”<%= session.getAttribute (“someValue”) %>” />

The JSP 2.0 specification allows the use of a standard expression language for all custom tag libraries.The expression language enables the author of a page to use a simpler syntax to access objects. For example:

<somePrefix:someCustomTag someAttribute=”${someValue}” />

As you can see from this line, expressions are enclosed inside curly braces {...}, and preceded with a $ character so that the JSP container knows to process the expression rather than treating it as literal text.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JSP Topics