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


  • 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=”” %>
  • XML (prefix: x)‏
    • Core, Flow control, Transformation
    • <%@ taglib prefix=”x” uri=”” %>
  • Internationalization (i18n) (prefix: fmt)‏
    • Locale, Message formatting, Number and date formatting
    • <%@ taglib prefix=”fmt” uri=”” %>
  • Database (prefix: sql)‏
    • SQL query and update
    • <%@ taglib prefix=”sql” uri=”” %>
  • Functions (prefix: fn)‏
    • Collection length, String manipulation
    • <%@ taglib prefix=”fn” uri=”” %>

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:

  1. <c:set var="foo" scope="session" value="..."/>  For example: <c:set var="bookId" value="${param.BookID}"/>
  2. <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 “” property value is “USA” are displayed through <c:forEach> loop.

<c:forEach var="customer" items="${customers}">
  <c:if test="${ == 'USA'}">


Performs conditional block execution by the embedded <c:when> and <c:otherwise> sub tags. Works like if-then-else.

Example: Here the font color is set differently depending on the value of “” property of a customer.
<c:forEach var="customer" items="${customers}">
    <c:when test="${ == 'USA'}">
      <font color="blue">
    <c:when test="${ == 'Canada'}">
      <font color="red">
      <font color="green"> 

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}">

Example: <c:forToken>

<c:forTokens var="token" items="one,two,three" delims=",">
<c:out value="${token}"/>

URL Management Tags

URL Import: <c:import>

It is more generic way to access URL-based resources (than <jsp:include>)‏. It uses Absolute URL for accessing resources outside of Web application and Relative URL for accessing resources inside of the same Web application.. <c:param> tag can be used to specify parameters (like <jsp:param>)‏


  <c:import url=""/>

Example: <c:import>  with <c:param>

<c:import url="header.jsp">
     <c:param name="pageTitle" value=""/>
     <c:param name="pageSlogan" value=" " />

URL Rewriting: <c:url>

It is used for URL rewriting. All the URL’s that are returned from a JSP page (to a browser) have session ID if Cookie is disabled on the browser. It can take param subtags for including parameters in the returned URL.

Example: <c:url>

<table border="1" bgcolor="#dddddd">
  <td>"base", param=ABC</td>
<c:url value="base">
         <c:param name="param" value="ABC"/>
  <td>"base", param=123</td>
         <c:url value="base">
           <c:param name="param" value="123"/>

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}">
      <td><c:out value="${customer.lastName}"/></td>
      <td><c:out value="${customer.phoneHome}" default="no home phone specified"/></td>
        <c:out value="${customer.phoneCell}" escapeXml="false">
          <font color="red">no cell phone specified</font>

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


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"
 <sql:query dataSource="${dataSource}" .../>

For 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 var="updateCount">
    INSERT INTO mytable VALUES (1,'Paul Oakenfold')‏
  <sql:update var="updateCount">
    INSERT INTO mytable VALUES (2,'Timo Maas')‏
  <sql:query var="deejays">
    SELECT * FROM mytable

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


<c:set var="xmlText">
<x:parse var="a" doc="${xmlText}" />
<x:out select="$a//c"/>
<x:out select="$a/a/d"/>


Works like <c:out> tag. <x:out> tag converts node-set type to a String type. The string value of the first node in the node-set. The string value of an element is the concatenation of all descendent text nodes, no matter how deep


          <td><pre><x:out select="$doc//sport"/></pre></td>
          <td><pre><x:out select="$doc/games/country/*"/></pre></td>
          <td><pre><x:out select="$doc//*"/></pre></td>
          <td><pre><x:out select="$doc/games/country"/></pre></td>

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
Tagged with: JAVAJAVA GUI

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.