20 - JSP Standard Tag Library (JSTL)

20.1 Overview Of JSP Standard Tag Library (JSTL)

JSTL stands for JavaServer Pages Standard Tag Library and is a set of tags which provides functionality common to many JSP. JSTL has tags such as iterators and conditionals for handling flow control, tags for manipulating XML documents, internationalization tags, tags for accessing databases using SQL, and commonly used functions.

JSTL has a vast range of tags and because if this we need not to search for any other third party libraries (in most of the cases).

JSTL tags are divided in following groups based on the set of functionality

a) Core tag library

b) XML tag library

c) JSTL functions

d) SQL tag library

e) Internationalization tag library (Formatting Tags)

20.2 JSTL Installation

You need to download jstl-1.2 jar file available at http://repo1.maven.org/maven2/javax/servlet/jstl/1.2/ (refer below figure) and place it in WB-INF\lib directory of web application.

20.3 Core Tag Library

· Core tag library provides functions related to variable support, flow control, URL management etc and provides around 14 tags.

· URI for core tag library is http://java.sun.com/jsp/jstl/core

· To use core tag library on JSP page we need to import its tag library using

   <%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>

Tags provided by core tag library are –

· <c:out> - this tag is used to write something in JSP page and is similar to expression tag <%= ... %>

· <c:import>-this tag is used to include other resources and is similar to <jsp:include> or @include directive.

· <c:redirect> - this tag is used to redirect request to another resource or URL.

· <c:set> - this tag is used to set the variable value in given scope.

· <c:remove>- this tag is used to remove the variable from given scope or from all scopes.

· <c:if> - this tag is used to implement conditions.

· <c:catch> - this tag is used to catch the exception and wrap it into an object.

· <c:choose>-This tag performs conditional block execution by the embedded when sub tags. It renders the body of the first when tag whose test condition evaluates to true. If none of the test conditions of nested when tags evaluates to true, then the body of an otherwise tag is evaluated, if present.

· <c:when> -this tag is sub tag of <c:choose> and its body gets executed if its condition evaluates to ‘true’.

· <c:otherwise>-this tag sub tag of <c:choose> that its body gets executed if all <c:when> condition evaluates to ‘false’.

· <c:forEach> - this tag is used for iteration over a collection.

· <c:forTokens>- this tag is used for iteration over tokens separated by a delimiter.

· <c:url> - this tag is used to create a URL with optional query string parameters.

· <c:param>- this tag is used with <c:url> and <c:redirect> to pass parameters.

20.3.1 <c:out> 

The <c:out> tag evaluates an expression and outputs the result to the current JspWriter object. The syntax and attributes are as follows:

     <c:out value="value" escapeXml="true|false" default="defaultValue"/>lue"/>

· escapeXml is an optional attribute and can take true or false with default value as true . This attribute controls if the characters will be escaped or not. If escapeXml is true then following characters will be escaped

< is converted to <

> is converted to >

& is converted to &

’ is converted to '

" is converted to "

· default is again an optional attribute and its value will be displayed if resulting value tag is returning null

· value is mandatory tag and supports expression language or static text as well.

this tag supports . (dot) notation to access the properties


Lets create cout.jsp with to demonstrate <c:out> tag like below

    <title> c:out tag example </title>
    <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
    Static Text example ::
    <c:out value="This is a static text "/>
    Expression Example without escapeXml ::
    <c:out value="${'<strong>This is an  example </strong>'}"/>
    Expression Example with escapeXml as false ::
    <c:out value="${'<strong>This is an example </strong>'}"escapeXml="false"/>
    Default value Example ::
      String variable = null;
    <c:out value="${variable}" default="This is default text"></c:out>

Access cout.jsp by hitting http://localhost:8080/jsp-tutorial/cout.jsp

If you look at view-source you can see the characters gets escaped (highlighted) when escapeXml attribute is set to true .

Like us on Facebook