Introduction to JSTLS2C Home « Introduction to JSTL
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 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 section 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 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 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 |
---|---|---|---|---|
2.1 | 1.2 | 2.5 | 6.0.x | 5 |
Downloading JSTLTop
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 version within the Maven repository allows you to download the 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
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.
The JSTL LibrariesTop
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 |
---|---|---|---|
Core | Variable Support Flow control URL management Miscellaneous | c | http://java.sun.com/jsp/jstl/core |
XML | Core Flow control Transformation | x | http://java.sun.com/jsp/jstl/xml |
Ii8n | Locale Message formatting Number and date formatting | fmt | http://java.sun.com/jsp/jstl/fmt |
Database | SQL | sql | http://java.sun.com/jsp/jstl/sql |
Functions | Collection length String manipulation | fn | http://java.sun.com/jsp/jstl/functions |
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
DirectiveTop
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="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head><title>Using JSTL</title></head>
<body>
<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>
</body>
</html>
The following screenshot shows the results of running the above code:
Lesson 1 Complete
In our first lesson on JSTL we downloaded JSTL 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.