Article
Introduction to the Java Standard Tag Library
In the beginning, there was HTML -- and everyone was happy. Then, one day, someone clever decided that they needed to have dynamic content on their Website, invented a scripting language that generated HTML, and all hell broke loose. Within months, Web pages had grown into a jungle of HTML, scripting code, and SQL statements.
At first, Java’s take on all this was the Servlet. Although useful in certain circumstances, it proved cumbersome and unwieldy. The saviour was to come in the form of Java ServerPages. Able to leap across buildings in a single bound, JSPs looked like a scripting language and acted like scripting language, but in actual fact were nothing more than a clever way of writing Servlets! JSPs solved a lot of problems that were inherent within Servlets, but they introduced a number of issues as well.
Pages could easily become a mess of scriptlet code and HTML fragments. Worse still, JSPs allowed you to avoid having to write JavaBeans to take care of your business logic; putting all your site functionality into the Web page itself was a quick fix that, more often than not, would come back and bite you when you least expected it. Finally, the typical front end developer took to JSP scripts like a fish to a bicycle, shying away from the complexities of Vectors, Request objects and null pointer exceptions in favour of simpler pleasures.
Why am I telling you all this? Because, just when it seemed like everything was headed in a downward spiral, there came a solution! Enter stage left, the Java Standard Tag Library.
Meet the JSTL
The Java Standard Tag Library (or JSTL) is nothing more than a Java Tag Library that provides us with a number of Standard functions. Ok, ok -- put another way, the JSTL is going to make your quagmire of JSP script and HTML look like regular plain old HTML pages again. And that is a good thing. Your pages will be more readable (no more scriptlets), your code architecture will become a lot cleaner (no more HTML in your JavaBean classes) and, best of all, the front end developers you work with will invite you to the pub at lunchtime like they used to.
Ok, just to whet your appetite before we talk about how to get the thing installed, here’s a fairly typical piece of code you might see in a JSP page:
<% For (Enumeration e = shoppingCart.getProducts();
e.hasMoreElements();) {
Product thisProduct = (Product)e.nextElement();
%>
<br /><%=thisProduct.getDescription()%>
<% } %>
Clear as mud, right? Here’s the same functionality written using the JSTL:
<c:forEach var="thisProduct" items="${shoppingCart.products}">
<br /><c:out value="${thisProduct.description}"/>
</c:forEach>
By now, the HTML developers you work with aren’t just inviting you to the pub, they’re getting the rounds in as well!
Ok, let’s get this thing installed. You will need an application server that adheres to the JSP 1.2 spec. For most people that means Tomcat 4.0+. Once Tomcat is installed, you’ll need to download a copy of the JSTL itself. You can get hold of a copy from the Jakarta Apache site (see Resources at the end of this article). Download the code for this article here.
- Create a new Web application in Tomcat (you can just create a new directory under the webapps/ directory). For this article, we’ll call the Web application “jstl_stuff”.
- Unpack the JSTL archive.
- Create a “WEB-INF” directory within “jstl_stuff”, and a “lib” directory within “WEB-INF”.
- Copy everything from the lib directory of your JSTL archive into <Tomcat Home>/webapps/jstl_stuff/WEB-INF/lib.
- Restart Tomcat.
To test the installation, create a page entitled index.jsp in the /jstl_stuff directory, and add the following code:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:out value="Doh! It's working!"/>
Now try browsing to http://localhost:8080/jstl_stuff. If you see a page that displays “Doh! It's working!” and nothing else, everything is working perfectly. If you see an explosion of java exceptions, something has gone wrong, and it’s time to start checking your Tomcat configuration.
Ben is Co-Director of