Article

Introduction to the Java Standard Tag Library

Page: 1 2 3 4 Next

The Components Explained

I’m going to be referencing the following class (simpsons.Homer) for the examples. You can compile it and place it in <tomcat home>/webapps/WEB-INF/classes/simpsons, or you could write your own.

package simpsons;  
import java.util.Vector;  
 
public class Homer {  
 private int iq;  
 private Vector braincells;  
 
 public Homer() {  
   iq = 24;  
   braincells = new Vector();  
   braincells.add("Eating");  
   braincells.add("Drinking");  
   braincells.add("Sleeping");  
 }  
 
 public int getIq() {  
   return iq;  
 }  
 
 public void setIq(int iq) {  
   if (iq < 100) {  
     this.iq = iq;  
   }  
 }  
 
 public Vector getBraincells() {  
   return braincells;  
 }  
}

Note: in the following examples, we need to declare that we’re going to use the Homer class in our JSP pages. Remember to add the following line to the top of your JSP pages after you’ve compiled the class:

<jsp:useBean id="homer" class="simpsons.Homer" scope="page" />

The JSTL is split into 4 components:

  1. Core - The main tags used within the library
  2. XML – A set of XML processing tags
  3. Format – Internationalization and formatting tags
  4. SQL – Tags for accessing an SQL Database

We are going to cover the Core and Format set of tags in this article. If you want to get to grips with either of the others, see the resource links at the end of this article.

In order to inform Tomcat that we are about to use the JSTL core tags in our page, we need to include the following taglib directive in every page in which JSTL tags are used:

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

As I mentioned earlier, the idea behind the JSTL is to provide a basic level of functionality to the programmer, so that JSP scriptlets are no longer needed. For example, if we want to assign a value to a variable, we can use the set action:

<c:set var="homer” value=”Doh!” />

To output something to HTML, we can use the out action:

<c:set var="homer” value=”Doh!” />  
<c:out value=”${homer}” />

Running this should provide us with a single “Doh!”

You can set the property of a JavaBean as well. For example, if wanted to set homer’s IQ property we could do:

<c:set target="${homer}" property="iq" value="34"/>

Veteran JSP coders will probably be scratching their heads at this point. This part of code might look unusual:

${homer}

Welcome to the JSTL Expression Language (or EL)! The EL is a method of accessing Java variables in a much simpler way than the old “JSP-style” involved. The EL is both simple and powerful. Using it, we can access the variables of an object in a wide variety of ways:

${homer.iq}

The above would access the homer object’s iq property.

${homer.braincell[1]}

The above would return the second element of the homer object’s braincell collection. Note that we don’t need to know (and, indeed, don’t care) whether the braincell property of the homer object is an array or a Vector (or anything else that implements the List interface); the EL does all the work for us.

We can also use the EL to evaluate expressions. So, for example, the following would evaluate to true:

${homer.iq < 50}

We can now start to use the EL and the JSTL tags to radically simplify our JSP pages.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article