Introduction to JSTL 1.2SJSP Home « Introduction to JSTL 1.2

In the previous two sections of this part of the site we have looked at technology to reduce Java scripting within our JSP pages, firstly when we looked at JavaBean Standard Actions and secondly when we looked at the Expression language in the Expression Language 2.1 section. Although these disciplines help reduce Java scripting they don't eradicate it from a JSP page completely as there is no way for example, to conditionally test a variable or iterate over a collection using these techniques alone.

In this section we learn how to remove Java scripting completely from our JSP pages by using JavaBeans and EL in conjunction with the JavaServer Pages Standard Tag Libraries 1.2 (JSTL) and we will be covering the 1.2 release. You need to have an understanding of Java and Servlets to get the most from these lessons, so if you're completely new to Java I suggest going to the Java 5 Tutor site and doing the lessons there first. If you're new to Servlets you would be better doing the lessons in the Sevlets 2.5 section of the site first.

To use JSTL we need a Java SDK; details of downloading and installing this are given in the Introduction to Servlets 2.5 lesson and a Servlet/JSP compliant web container to host our JSPs on; details of downloading and installing this are given in the Getting Tomcat 6 lesson. The following table shows the correalation between the JSP, EL, JSTL, Servlets, Tomcat and Java versions used in these lessons:

JSP/EL JSTL Servlets Tomcat Version Minimum Java Version

Downloading JSTLgo to top of page Top

The following link will take you to the download page for the Bundled JSTL API and JSTL Implementation where you can download the jstl-1.2.jar JAR, JSP Standard Tag Library.

Clicking on the the JSTL 1.2 version within the Maven repository allows you to download the JAR.

Download JSTL JAR

The JAR must be copied into the WEB-INF/lib library of every web application that requires JSTL, or into the library directory of the container you're using, to allow JSTL for everything; for my Tomcat 6 installation this is C:\apache-tomcat-6.0.37\lib

The following screenshot shows the JAR needed on my Windows7 operating system and placed in the C:\apache-tomcat-6.0.37\lib folder. This makes JSTL accessible by all JSP pages which may or may not be the way you choose to allocate the resource.

Download JSTL JAR

The JSTL Librariesgo to top of page Top

The JSTL is subdivided into 5 areas where actions are grouped together by category into five tag libraries; these being Core, XML, I18N, Database and Functions libraries. The following table shows the tag libraries and the functional areas within then, along with the preferred prefix and the URI location:

Tag Library Functional Areas Prefix URI
CoreVariable Support
Flow control
URL management
Flow control
Message formatting
Number and date formatting
FunctionsCollection length
String manipulation

We will look at each of the tag libraries in much greater detail over the next five lessons and also at some of the tags within each. Click a tag library link in the table above to go to the lesson required.

The taglib Directivego to top of page Top

We use the taglib directive to import a tag library into a JSP page. The taglib directive syntax follows:

<%@taglib uri="uri"  prefix="prefix" %>

Although the prefix can be any string literal by convention the prefixes shown in the JSTL Libraries table above are used. This makes the tag library being used easily identifiable to other developers.

The following code snippet is an example of importing the Core tag library into a JSP page and replacing the Java scripting we used in a previous lesson with a JSTL tag. Don't worry about the syntax for now as we will cover tag syntax for specific tags over the next five lessons.

<%@taglib uri="" prefix="c" %>

<!DOCTYPE html>
  <head><title>Using JSTL</title></head> 
    <h1>Setting Some Attributes With JSTL</h1> 
    <c:set var="a" value="5" scope="request" />
    <c:set var="b" value="15" scope="request" />
    <p>Expression output: ${a+b}</p>

The following screenshot shows the results of running the above code:

run first JSTL JSP

Lesson 1 Complete

In our first lesson on JSTL 1.2 we downloaded JSTL 1.2 and took a brief look at the five tag libraries we can use.

What's Next?

In our first lesson on JSTL tag libraries we look at the Core tag library.

go to home page Homepage go to top of page Top