JSP Standard Tag Library (JSTL)
The JSP Standard Tag Library (JSTL) is a very important component released by Oracle for JSP programming. JSTL allows us to program our JSP pages using tags, rather than the scriptlet code that most JSP programmers are already accustomed to. JSTL can do nearly everything that regular JSP scriptlet code can do.
Advantages of JSTL—
- Rapid development: JSTL provides several tags to enable faster development of JSP pages.
- Code re-usability: We can use the JSTL tags on many pages.
- Scriptlet-free tag: It avoids the use of scriptlet tag in JSP pages.
JSTL Example
JSTL was introduced to write JSP programs using tags rather than Java code. To show why this is preferable, a quick example is in order. We will examine a very simple JSP page that counts to five. We will examine this page both as regular scriptlet-based JSP, and then as JSTL. When the “count to five example” is programmed using scriptlet-based JSP, the JSP page (count.jsp) appears as follows.
<html> <head> <title>Count to 5 in JSP scriptlet</title> </head> <body> <% for(int i=1; i<=5; i++) { %> <%= i %> <br/> <% } %> </body> </html>
As we can see from the preceding example, using scriptlet code produces page source code that contains a mix of HTML tags and Java statements. There are several reasons why this mixing of programming styles is not optimal.
The primary reason that it is not optimal to mix scriptlet and tag-based code is readability. This readability appeals both to humans and computers. JSTL allows the JSP programmer to look at a program that consists entirely of HTML and HTML-like tags.
The readability of JSP scriptlet code does not just apply to human beings. The mixing of scriptlet and HTML code is also hard for computers to read. This is particularly true of HTML authoring tools such as Adobe Dreamweaver and MS FrontPage. Currently, most HTML authoring tools will segregate JSP scriptlet code as non-editable blocks. The HTML authoring tools usually do not modify the JSP scriptlet code directly.
The following code shows how the “count to five example” would be written using JSTL. As we can see, this code listing is much more constant, as only tags are used. HTML and JSTL tags are mixed to produce the example (count_new.jsp).
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Count to 5 Example (using JSTL)</title>
</head>
<body>
<b>Using JSTL</b><br>
<c:forEach var="i" begin="1" end="5" step="1">
<c:out value="${i}" />
<br />
</c:forEach>
</body>
</html>
How to download and install JSTL
- Download jstl-1.2.jar.zip file from here (or you can get it from Apache tomcat website).
- Now put the file into your ‘WEB-INF/lib’ folder.
- Then, you can use JSTL into your project.
Output:
When we examine the preceding source code, we can see that the JSP page consists entirely of tags. The above code makes use of HTML tags such <head> and <br>. The use of tags is not confined just to HTML tags. This code also makes use of JSTL tags such as <c:forEach> and <c:out>. In this section I will introduce some of the basics of JSTL. The example codes are placed within Jstl-Expt directory under webapps.
In the next topic I will introduce some of the basics of JSTL.
JSTL Tag Libraries
JSTL is often spoken of as a single-tag library. JSTL is actually four tag libraries. These tag libraries are summarized as follows.
- Core Tag Library— Contains tags that are essential to nearly any Web application. Examples of core tag libraries include looping, expression evaluation, and basic input and output.
- XML Tag Library— Contains tags that can be used to access XML elements. Because XML is used in many Web applications, XML processing is an important feature of JSTL.
- Internationalization Tag Library— Contains tags that are used to and parse data. Internationalization (I18n) and general formatting are supported by the actions in the I18N & Formatting library. Some of these tags will parse data, such as dates, differently based on the current locale.
- Database Tag Library— Contains tags that can be used to access SQL databases. These tags are normally used only to create prototype programs. This is because most programs will not handle database access directly from JSP pages. Database access should be embedded in EJBs that are accessed by the JSP pages.
- Function Tag Library— Contains tags that can be used for common String manipulation.
The following table lists each library with its recommended tag prefix and default URI:
Table 1: JSTL Tags
Description | Prefix | Default URI |
Core | c | http://java.sun.com/jsp/jstl/core |
XML Processing | x | http://java.sun.com/jsp/jstl/xml |
I18N & Formatting | fmt | http://java.sun.com/jsp/jstl/fmt |
Database Access | sql | http://java.sun.com/jsp/jstl/sql |
Functions tag library | fn | http://java.sun.com/jsp/jstl/functions |
In this topic, we will only take a brief look at a few of the core tags. We will examine a simple example that shows how to process data that a user enters into a form. Before we examine this program, we must first see how JSTL handles expressions. Expression handling in JSTL is accomplished by using the EL expression language, just as it is done in JSP 2.0. In the next section, we will examine the EL expression language.
EL Expression Language
One major component of JSP 2.0 is the new expression language named EL. EL is used extensively in JSTL. However, it is important to remember that EL is a feature of JSP and not of JSTL. JSP scriptlet code used with JSP 2.0 can contain EL expressions. The following lines of code demonstrate using EL inside of JSP scriptlet code.
<p> Your total, including shipping is ${total+shipping} </p>
As we can see form the preceding code, the values “total” and “shipping” are added and displayed as the HTML is generated. These expressions can be used inside of JSTL tags as well. One important requirement of JSTL 1.0 was that JSTL could be used with JSP 1.2.
Because JSP 1.2 does not support EL, it is necessary to provide a few additional JSTL tags that facilitate the use of EL. For example, if we want to use JSTL to display the above expression, we would use the following code:
<p> Your total, including shipping is <c:out var="${total+shipping"/> </p>
One of the requirements of JSTL was that it not requires JSP 2.0 to run. By providing a tag that is capable of displaying EL expressions, this requirement is met.
Another JSTL Example
We will now examine a simple example that uses JSTL. For this example, we will examine a common procedure that is done by many Web applications. We will see how to POST a form and process the results from that POST. A simple program (test.jsp) that is capable of doing this is shown below.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>If with Body</title> </head> <body> <body> <c:if test="${pageContext.request.method=='POST'}"> <c:if test="${param.guess=='Java'}"> Bravo! You guessed it. <br /> <br /> <br /> </c:if> <c:if test="${param.guess!='Java'}"> Alas! You are wrong. <br /> <br /> <br /> </c:if> </c:if> <form method="post">Guess what computer language I am thinking of? <br /><br /> <input type="text" name="guess" /> <input type="submit" value="Try!" /> <br /><br /> </form> </body> </body> </html>
This simple web page will display a form and ask the user to guess what computer language the program is thinking of. Of course, the computer is thinking of “Java.” This page begins by checking to see if a POST was done. This allows both the form, and the code that handles the form, to be placed on one single page. This is done with the following JSTL if statement.
<c:if test="${pageContext.request.method=='POST'}">
Here we can see that the <c:if> tag uses an EL expression to evaluate whether the request method is POST. If data was posted to the page, the value that the user entered for their guess is stored in a parameter named “guess”.
This is because “guess” was specified as the name of the form input item. We must now check to see whether this parameter is equal to the word “Java”. This is done with the following <c:if> tag.
<c:if test="${param.guess=='Java'}">Bravo! You guessed it. </c:if>
As we can see, the body of the <c:if> tag is executed if the statement evaluates to true.
The output is shown below:
On clicking the button named “Try!” will produce the following output:
In this topic, we began to examine the basics of how JSTL is installed and how it works. There is much more to JSTL than the small example we examined in this topic. The core tags of JSTL also include tags for looping, iteration, and variable handling. By using these tags, we can iterate through collections, access user session data, and perform other core tasks that all Web applications perform. In addition to the core tag library, the XML, database, and formatting tag libraries are also provided for more advanced uses.
This topic shows the differences between the JSTL and standard JSP scriptlet programming. As we can see, JSTL allows a more consistent programming environment by allowing both HTML and procedural code to be expressed as tags. JSTL and tag libraries represent a new method of programming Web pages.
JSTL does not provide everything that a programmer would need to create a full-featured Web application. Further, some procedures that could be programmed in JSTL is often best not programmed in JSTL. One perfect example of this is the database JSTL tags.
Except for very small Web applications, it is generally considered bad programming practice to embed actual database commands into a JSP page. The proper location for such program code is in Java beans and EJBs that will be used by our Web application. For such routines, we may consider creating our own tag libraries.
This way, our JSP pages can use JSTL to perform basic programming procedures that are not unique to our business. We should implement our own tag libraries to implement components, which are unique to our business, which will be used by our Web application.