JSP Standard Tag Library (JSTL)
The JavaServer Pages Standard Tag Library (JSTL) is a standard set of commonly used tag libraries. That is, JSTL encapsulates core functionality common to many JSP applications. For example, instead of iterating over lists using a scriptlet or different iteration tags from numerous vendors, JSTL defines a standard set of iteration tags. JSTL has tags for common structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and tags for accessing databases using SQL.
Looking for JSP Custom Tags
Why JSTL?
- You don’t have to write them yourself
- You learn and use a single standard set of tag libraries that are already provided by compliant Java EE platforms
- Vendors are likely to provide more optimized implementation
- Portability of your applications are enabled
JSTL Tag Libraries
So JSTL 1.1 have categorized the standard tags into 5 different functional areas:
- Core (prefix: c)
- Variable support, Flow control, URL management
-
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
- XML (prefix: x)
- Core, Flow control, Transformation
- <%@ taglib prefix=”x” uri=”http://java.sun.com/jsp/jstl/xml” %>
- Internationalization (i18n) (prefix: fmt)
- Locale, Message formatting, Number and date formatting
- <%@ taglib prefix=”fmt” uri=”http://java.sun.com/jsp/jstl/fmt” %>
- Database (prefix: sql)
- SQL query and update
- <%@ taglib prefix=”sql” uri=”http://java.sun.com/jsp/jstl/sql” %>
- Functions (prefix: fn)
- Collection length, String manipulation
-
<%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
Core Tags
Core tags themselves are categorized into several types – tags that are used for setting and removing scoped variable, conditional tags such as <c:if>, iteration tags such as <c:forEach>. Following are tags and their names:
- Variable support
- <c:set>
- <c:remove>
- Conditional
- <c:if>
- <c:choose>
- <c:when>
- <c:otherwise>
- Iteration
- <c:forEach>
- <c:forTokens>
- URL management
- <c:import>
- <c:param>
- <c:redirect>
- <c:param>
- <c:url>
- <c:param>
- General purpose
- <c:out>
- <c:catch>
Variable Support, <c:set>
The set tag sets the value of an EL variable or the property of an EL variable in any of the JSP scopes (page, request, session, application). The name of the variable is set via “var” attribute and the scope of the variable is set via “scope” attribute.
Variable can be set in 2 different ways:
-
<c:set var="foo" scope="session" value="..."/> For example: <c:set var="bookId" value="${param.BookID}"/> -
<c:set var="foo">value to be set</c:set>
Variable Support <c:remove>
To remove an EL variable, you use the remove tag. When the bookstore JSP page bookreceipt.jsp is invoked, the shopping session is finished, so the cart session attribute is removed as follows:
<c:remove var="cart" scope="session"/>
Conditional Tags
Flow control tags eliminate the need for scriptlets. Without conditional tags, a page author must generally resort to using scriptlets in JSP page
<c:if test=”..”>
Conditional execution of its body according to value of a test attribute
Example: Only the customers whose “address.country” property value is “USA” are displayed through <c:forEach> loop.
<c:forEach var="customer" items="${customers}">
<c:if test="${customer.address.country == 'USA'}">
${customer}<br>
</c:if>
</c:forEach>
<c:choose>
Performs conditional block execution by the embedded <c:when> and <c:otherwise> sub tags. Works like if-then-else.
<c:forEach var="customer" items="${customers}">
<c:choose>
<c:when test="${customer.address.country == 'USA'}">
<font color="blue">
</c:when>
<c:when test="${customer.address.country == 'Canada'}">
<font color="red">
</c:when>
<c:otherwise>
<font color="green">
</c:otherwise>
</c:choose>
${customer}</font><br>
</c:forEach>
Iterator Tag
The Iterator tag <c:forEach> allows you to iterate over a collection of objects. You specify the collection via the “items” attribute, and the current item is available through a variable named by the “var” attribute. You can also get the iteration status via “varStatus” attribute. The range and interval can be also specified via “begin”, “end”, and “step” attributes.
<c:forEach var="customer" items="${customers}">
${customer}<br>
</c:forEach
Example: <c:forToken>
<c:forTokens var="token" items="one,two,three" delims=",">
<c:out value="${token}"/>
</c:forTokens>
URL Management Tags
URL Import: <c:import>
Example:
<blockquote> <ex:escapeHtml> <c:import url="http://www.cnn.com/cnn.rss"/> </ex:escapeHtml> </blockquote>
Example: <c:import> with <c:param>
<c:import url="header.jsp"> <c:param name="pageTitle" value="newInstance.com"/> <c:param name="pageSlogan" value=" " /> </c:import>
URL Rewriting: <c:url>
Example: <c:url>
<table border="1" bgcolor="#dddddd"> <tr> <td>"base", param=ABC</td> <td> <c:url value="base"> <c:param name="param" value="ABC"/> </c:url> </td> </tr> <tr> <td>"base", param=123</td> <td> <c:url value="base"> <c:param name="param" value="123"/> </c:url> </td> </tr> <tr>
Redirection: <c:redirect>
The redirect tag sends an HTTP redirect to the client. The redirect tag takes param subtags for including parameters in the returned URL.
General Purpose Tags
Example: <c:out>
<table border="1">
<c:forEach var="customer" items="${customers}">
<tr>
<td><c:out value="${customer.lastName}"/></td>
<td><c:out value="${customer.phoneHome}" default="no home phone specified"/></td>
<td>
<c:out value="${customer.phoneCell}" escapeXml="false">
<font color="red">no cell phone specified</font>
</c:out>
</td>
</tr>
</c:forEach>
</table>
Database Access Tags
In production environment, the database access logic is typically captured within JavaBeans which functions as a Model under MVC architecture. This state of the JavaBeans is set by the Controller (Servlet) and then is accessed by the JSP pages (View).

DataSource
All DB actions operate on a DataSource. Different ways to access a DataSource:
- Object provided by application logic
- Object provided by <sql:dataSource> action
<sql:dataSource var="dataSource"
driver="org.gjt.mm.mysql.Driver"
url="jdbc:..."/>
<sql:query dataSource="${dataSource}" .../>
For Example:
<sql:setDataSource var="example" driver="com.pointbase.jdbc.jdbcUniversalDriver" url="jdbc:pointbase:server://localhost:1092/jstlsample;create=true" />
Example:
This example shows the creation of a very simple database and then populating the table. Then querying the database table and save the query result into a scope variable “deejays”.
<sql:transaction dataSource="${example}">
<sql:update var="newTable">
create table mytable (
nameid int primary key,
name varchar(80)
)
</sql:update>
<sql:update var="updateCount">
INSERT INTO mytable VALUES (1,'Paul Oakenfold')
</sql:update>
<sql:update var="updateCount">
INSERT INTO mytable VALUES (2,'Timo Maas')
</sql:update>
...
<sql:query var="deejays">
SELECT * FROM mytable
</sql:query>
</sql:transaction>

XML Tags
XML tags are used to access information stored in XML document. Now what is unique about XML tags is that the access description is specified in XPath expression as a value of “select” attribute as shown in the slide above. Except that the access description is described in XPath expression, the flow control, iteration, and general purpose tags work similarly as corresponding tags in Core tags.
- Flow control
- <x:choose>, <x:when>, <x:if>, <x:otherwise>
- Iteration
- <x:forEach>
- General purpose
- <x:out>
- <x:set>
- Parsing and Transformation
- <x:parse>
- <x:transform> with <x:param> subtags
We’ll discuss only <x:parse> and <x:out> tags here.
<x:parse>
<x:parse> tag is for parsing XML document into a scoped varilable. This requires x-path queries that is beyond the scope of this article. If you don’t understand this, then please leave <x:parse> here and move to the next heading.
Example:
<c:set var="xmlText">
<a>
<b>
<c>
foo
</c>
</b>
<d>
bar
</d>
</a>
</c:set>
<x:parse var="a" doc="${xmlText}" />
<x:out select="$a//c"/>
<x:out select="$a/a/d"/>
<x:out>
Example:
<tr> <td>$doc//sport</td> <td><pre><x:out select="$doc//sport"/></pre></td> </tr> <tr> <td>$doc/games/country/*</td> <td><pre><x:out select="$doc/games/country/*"/></pre></td> </tr> <tr> <td>$doc//*</td> <td><pre><x:out select="$doc//*"/></pre></td> </tr> <tr> <td>$doc/games/country</td> <td><pre><x:out select="$doc/games/country"/></pre></td> </tr>

EL Functions
Following list shows the EL functions that you can use in JSP.
- <fn:length> Length of collection of string
- <fn:toUpperCase>, <fn:toLowerCase> Change the capitalization of a string
- <fn:substring>, <fn:substringBefore>, <fn:substringAfter> Get a subset of a string
- <fn:trim> Trim a string
- <fn:replace> Replace characters in a string
- <fn:indexOf>, <fn:startsWith>, <fn:endsWith contains>, <fn:containsIgnoreCase> Check if a string contains another string
- <fn:split>, <fn:join> Split a string into an array,and join a collection into a string
- <fn:escapeXml> Escape XML characters in the string
Formatting Tags
JSTL provides a set of tags for parsing and formatting locale-sensitive numbers and dates.
- <fmt:formatNumber>, <fmt:formatDate>
- used to output localized numbers and dates
- <fmt:parseNumber>, <fmt:parseDate>
- used to parse localized numbers and dates
- <fmt:setTimeZone>, <fmt:timeZone >
- used to set and get timezone
Related Posts
Popular Posts (last 30 days)
- Attendance Management System 1453 view(s)
- Advanced Java Tutorial (For Intermediate) 757 view(s)
- JAVA Graphical User Interface (GUI) 714 view(s)
- Graph Implementation in C++ 521 view(s)
- File Handling using Input-Output Streams in Java 471 view(s)
- Linked lists in C++ 469 view(s)
- Sockets and Network Programming in Java 390 view(s)
- Applications of Stack in data structures 388 view(s)
- UDP Datagram Sockets in Java 385 view(s)
- Circular Linked Lists 344 view(s)







