What is JSTL (JSP Standard Tag Library)

is a collection of JSP custom tags developed by Java Community Process, www.jcp.org. The reference implementation is developed by the Jakarta project, jakarta.apache.org. JSTL offers tags through 4 libraries:

* core - Basic scripting functions
* xml - XML processing
* fmt - Internationalization of formatting
* sql - Data base accessing

We add them this way:

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

Hello World! with JSTL:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:out value="Hello world!"/>

What is EL (Expression Language)

Before JSP 2.0, you could use only a scriptlet, JSP expression, or a custom tag to include server state in the JSP page output. Although these solutions are useful, they require a relatively large amount of work for even the simple job of accessing server-side state. JSP 2.0 incorporates the Expression Language (EL) first introduced in JSTL 1.0. So EL is an extension of JSTL.

A JSP 2.0 EL expression is always written between inside ${ … }.

    <c:forEach var="k" items="${colors}">
        <li><font color="${k}">This line is ${k}</font>
    <c:forEach var="k" items="${events}">
        <li><c:out value="${k.title}"/> </li>
    <c:if test="${i.verified}"><img src="img/verified.png"/></c:if>    <!-- if 'i.verified' is evaluated as 'true' -->
    <c:if test="${i.verified=='true'}"><img src="img/verified.png"/></c:if>
    <c:if test="${user != null}" >
             Hi, <c:out value="${user.firstname}"/>  
    <c:if test="${not empty paidPromotions}">Hello</c:if>

Property Access

Dot operator (.) can be used to retrieve a named property. To access numbered or named properties use the bracket operator ( [] ).
For example: ${user.name}, ${row[0]} or ${order.items.price}

Use the empty operator to determine whether a collection or string is empty or null . Example: <c:if test="${empty pageScope.users}">

Binding one item in a list (drop down) to a known value as selected

Notice that orderStatusValues (list of objects) is a list and order is another variable (object) from a servlet. We want to match the corresponding item from the list to the value of order.status and show it as the default selected item.

                <select name="status">
                    <c:forEach items="${orderStatusValues}" var="status">
                        <option value="${status}" ${order.status == status ? 'selected' : ''}>${status}</option>

If we dont do this then we need to write a lot of ugly scriptlets.

More Reading


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License