How do I use JSTL on my JSPs?
Author: Deron Eriksson
Description: This Java tutorial describes how to use JSTL on JSPs.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page:    1 2 >

This tutorial was created with using TomcatSW 5.5.20, which is a JSPW 2.0 servletW container, which simplifies JSTLSW development compared to earlier JSP specifications.

JSTL stands for JavaServer Pages Standard Tab Library. It is a set of JavaSW tag libraries that simplify coding on JSPsW, giving your JSP page a tag (like HTMLW and XMLW) look-and-feel rather than a scriptlet/Java look-and-feel. Personally, I think JSTL is a great tool for performing tasks such as iterating over relatively simple data structures and similar tasks. Some developers act as if scriptletS code is evil, but in my opinion, scriptlets do have their place. In my opinion, JSTL is great for simple stuff while scriptlet code is best for stuff that would be extremely difficult to try to do in JSTL.

In this tutorial, I'll show how to set up JSTL for your application, and describe a little about how JSTL works.

The JSTL jarW files are hosted on the ApacheSW Jakarta Taglibs site, located at http://jakarta.apache.org/taglibs/. At the time of this writing, you can download the JSTL jars at http://jakarta.apache.org/site/downloads/downloads_taglibs-standard.cgi. Within the zip file that you can download are two jar files, jstl.jar and standard.jar.

Add jstl.jar and standard.jar to the WEB-INF/lib directory of your web application project, and include them in your project's classpathW while you work on your project. In the old days, you would need to add 'tld' entries to your web.xmlW file. However, these days, this is unnecessary with the current JSP specification, since the tag libraries can actually be found within the jar files.

Let's create a jspW called 'jstl-test.jsp' within our web directory.

jstl-test.jsp

On your jsp, you need to specify which JSTL tag libraries you would like to reference on your jsp. This tutorial utilizes the JSTL 1.0 core library tags, which can be declared on your jsp as follows:

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

NOTE: If you'd like to use Expression Language (EL) in your JSTL, use the taglibs below instead (which are contained in the same standards.jar file as the the other taglibs). These are the JSTL 1.1 core library tags.

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

To keep things simple, let's add all the taglib references to our jstl-test.jsp.

One of the nice features of the EclipseSW WTP is that the JSP editor features code completion (control-space) of tag libraries. If you hit 'control-space' it displays the various tag and attribute options that you have for the various tag libraries at the cursor position. It also features color-coding of the various page elements.

Eclipse Code Assist for tag libraries

Let's tell the c:out JSTL tag to output "This is JSTL".

c:out tag

The final jstl-test.jsp is shown below.

jstl-test.jsp

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

Test Page<br/>
<c:out value="This is JSTL"/>

If we hit this JSP page in our browser, we see the following:

displaying page in browser

(Continued on page 2)

Page:    1 2 >