Skip to content

JSTL

Getting to the good stuff finally.

What’s JSTL?

  • Java Standard Tag Library (JSTL)
  • JSTL is a set of custom tags that when combined with the EL solve almost all web application presentation demands and challenges.
  • Example: Oooo, a foreach loop!
<c:forEach var="employee" items="${employeeList}">
  <tr>
    <td>${employee.id}</td>
    <td>${employee.firstName}</td>
    <td>${employee.lastName}</td>
  </tr>
</c:forEach>
  • JSTL provides (among many other things)
    • foreach looping through all types of data collections
    • if logic.
    • variable manipulation.

JSTL Libraries

The JSTL libraries API docs - http://download.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/

There are 5 libraries

  • JSTL Core
  • JSTL fmt
  • JSTL sql
  • JSTL XML
  • JSTL functions

Taglib Descriptor

To use JSTL, you have to have this on the page.

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

The prefix, "c", can be changed to resolve conflicts with other tag libraries.

Basic JSTL Usage

An xml tag with the prefix as namespace:

    <c:tag  ... >

Attributes are used based on the tag:

<c:set var="name" value="Fred" scope="session" />

JSTL tags can be empty or can contain other JSTL tags, HTML tags, or other text.

<c:if test="${isLoggedIn == true}" >
  <h3>Welcome!</h3>
</c:if>

JSTL Core Tags

We’ll be learning the core tags. Here’s a list of them.

  • c:catch
  • c:choose
  • c:forEach
  • c:forTokens
  • c:if
  • c:import
  • c:otherwise
  • c:out
  • c:param
  • c:redirect
  • c:remove
  • c:set
  • c:url
  • c:when

The tags we’ll mainly use this semester are:

  • c:choose
    • c:when
    • c:otherwise
  • c:forEach
  • c:if
  • c:set

JSTL Core Tags Examples

Setting a variable and using it later

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

<!DOCTYPE html>
<c:set var="name" value="Fred" />
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <p>
      Hi, ${name}!
    </p>
  </body>
</html>

A servlet setting a value…

    public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {

        HttpSession  session  = request.getSession();

        session.setAttribute("city", "Madison");

        ...
}

And then a JSP using it

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

<!DOCTYPE html>

<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <p>
      City: ${city}
    </p>
  </body>
</html>

A servlet creating a list of values…

public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {

    HttpSession  session  = request.getSession();

    List flowers = new ArrayList();
    flowers.add("Tulip");
    flowers.add("Rose");
    flowers.add("Daffodil");
    flowers.add("Petunia");
    flowers.add("Lily");

    session.setAttribute("flowersList", flowers);

    ...

}

And then a JSP using it to create an HTML unordered list. Be careful! The <c:forEach ...> tag is case sensitive. This is XML afterall.

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

<!DOCTYPE html>

<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <ul>
      <c:forEach var="flower" items="${flowersList}">
        <li>${flower}</li>
      </c:forEach>
    </ul>
  </body>
</html>

The HTML output from above:

<!DOCTYPE html>

<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <ul>
        <li>Tulip</li>
        <li>Rose</li>
        <li>Daffodil</li>
        <li>Petunia</li>
        <li>Lily</li>
    </ul>
  </body>
</html>