Today is a good day to code

How the JSTL Could Save My Life

Posted: December 31st, 1969 | Author: | Filed under: Companies, Sun Microsystems, Uncategorized | Tags: | No Comments »

How the JSTL Could Save My Life

Picture of Irv Owens Web DeveloperThe JSTL is short for Java Standard Tag Library. What it does is to take common tasks done by web developers and make them easily callable by using a standard xml style tag within a HTML / XHTML / XML page. This is great because using JSP at and before 1.0 was often difficult because developers had to embed entire classes in web pages, or create external classes and link to them. JSP 1.1 introduced the JSTL and made some other improvements like being able to write code fragments, or Java without having to follow all of the usual rigors of writing Java.

One could create a function or method without having to wrap it in a class. Perhaps some of you are thinking, “Why would anyone want to pervert Java in this fasion, all this would lead to is a bunch of unmaintainable code.” The people who are thinking this probably already have a good library of Java classes laying around that they can call on to do simple tasks.

The first time I wrote “Hello World” as a JSP page the old fashioned way I immediately went back to PHP and ColdFusion. It just didn't make any sense to write all that code simply to print one fragment of a sentence “Hello World.” If I remember correctly, it took something like four to seven properly formatted lines to get a single line of output. To a developer that has been doing ColdFusion or PHP, this is ludicrous. So, let's compare the JSTL with ColdFusion and PHP and you'll see why I am so pumped about it!

To begin, I downloaded the netBeans 4.0 IDE from Sun. This is probably the best Java IDE around. It allows for visual J2SE development, for desktops, making creating interfaces a breeze, admittedly a tough point for me, it also assists in JSP development. A development copy of Jakarta Tomcat is installed inside the environment so you don't have to have a JSP wrapper installed. In addition to the usual help documents there are sample applications included in the download. You simply have to create a new project of the type sample, and select the JSTLSample project. This will create a plethora of code and examples for you to browse at your leisure.

One of the tough points was that initially I couldn't get my JSP to work. The reason I found was because I didn't have the JSTL class files in the right place, and I didn't really know where to look. Fortunately, I had the samples as an example, and I was able to figure it out. I got everything to work by downloading the proper files for the JSTL from the jakarta website.

At first, I didn't know where to put them, but after some poking around I discovered that you had to put them in a folder called “lib” under the “WEB-INF” subfolder in the project. There were a bunch of files in there after I unpacked the downloaded archive, and I didn't really like that, so I kept looking and found out that all those jar files could be archived into one massive jar file. I got that out of one of the tomcat “ROOT” folders that were part of another intallation. Anyway, once I got that in place, I was ready to go. All JSP pages that are going to use the JSTL need to call it out by pulling in a taglib.

There are several taglibs that provide the funcationality for the ever expanding JSTL standard library, and there are custom taglibs created by some very intrepid JSP developers of which I hope to be someday. The way you call out the most basic JSP taglib is as follows:

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

This initializes the core JSTL library which gives you commands to iterate over a dataset, set variables, and evaluating expressions. The other libraries include a functions library that does things like localization and finding and replacing substring elements with other values. It also includes a sql library that has everything a developer would need for setting up a data connection to a database using a JDBC driver. You can set the datasouce, then you can pass SQL to the server and handle the returning dataset. There is a formatting library that will allow you to do date and number formatting. The functionality is very good for such a young technology.

There is some overhead in processing the tags vs raw Java, but it is negligable, and with the great increases in code readibility I am sure we all can agree it is worth it. Now, a code example of JSP using JSTL. This is the first JSP page I ever wrote.


${i}

That's it. This code will, in a HTML body, count from 1 to 100. To compare that to ColdFusion:


#i#

Kinda looks the same doesn't it. It is pretty obvious that the developers working on JSP are going for the same ease of use and readibility that ColdFusion provides as well as allowing for complex Java coding in the back end. Of course as with ColdFusion, you can make your own Java tags that perform whatever task you can dream up.

With some community support, the JSTL could become every bit as functional as ColdFusion, and remain pretty much open source. The great thing here is that on can maintain more control over their source as they are distributing compiled files that have their Java code inside of it instead of ColdFusion templates that can be opened in any text editor revealing all the juicy proprietary code inside.

The reason the JSTL will save my career is that I have not seen any public statement from Adobe about what they intend to do with ColdFusion, and with the JSTL I am less concerned, because I know that I can ramp up on it quickly since I have been working on Java for the better part of a year, and I can produce clean readable code. The ability to extend the JSTL with ease is also very appealing especially for custom server functions for web applications. I'd say that JSP's future looks very bright indeed, and we'll all continue to hope for the best for ColdFusion

Here are some links for more info on JSP

Jakarta's Taglibs
Good Article from Sun Microsystems on the JSTL
A good article from JavaWorld on the JSTL