The Power of Simple Tags
Simple tags, in contrast to classic tags, are a new feature in JSP 2.0 with a huge positive impact on the usefulness of this technology. In short, simple tags allow you to abstract functionality and refactor template data in JSP, and undoubtedly, refactoring and abstraction is what programming is all about. Since simple tags are nothing but JSP files (or even better, XML documents, if you are wise enough to always use JSP documents based on XML technology), they allow page authors who are not familiar with the Java programming language to use the vast capabilities of Java and JSP.
Here I am going to explain simple tags through an example. I will present a complete JSP application (though very simplistic) to visualize the use of simple tags. In this example, I presume we use Tomcat 5.5 (based on JDK 5.0). The
test application is placed in
TOMCAT/webapps/newtest/) directory and its subdirectories, where
TOMCAT must be replaced with the directory where you have installed Tomcat. The two simple examples presented here can be viewed at URLs:
http://localhost/newtest, provided you have configured Tomcat to use port 80. It should be remembered that JSTL JAR files (
jstl.jar) must be accessible to this application, e.g. must be placed in
Consider the first version of this simplistic application:
Now, simple tags can do 2 things for us here:
- Abstracting functionality. Consider creating a hyperlink. First you create the URL using
<c:url />tag from JSTL core library, and store it in a variable. Then you use the value of that variable in an HTML
<a />tag. This can be made easier using simple tags, as you will see briefly.
- Refactoring template data. Both pages contain some common static text data that can be refactored using simple tags.
The new version of our program uses simple tags to solve these two problems conveniently.
This was barely more than a "Hello World!" app, but here are two screenshots of the pages to show that it works properly:
The code archive for this example can be found here.
- Even though I don't have plenty of visitors, but the few people who have been visiting this page are always a source of encouragement for me. I am especially grateful to those few who have written comments or have sent me emails in this regard. But some of these folks have been asking me questions about the Java programming language to whom I have barely had time to respond. It should be noted that I do not claim to teach Java -- in fact, I think I have an innate deficiency in teaching abilities! As a pediatric practitioner, I scarcely have time to do that. This is in fact an apology to all of those who have written to me and have not received any response yet. I do appreciate your interest and correspondence, but I may not be able to reply to your emails in the near future.
- Recently, I was informed that SustainableGIS company has created a testing page for my persiancalendar Java program. This is the first use of this program in the real world that I am aware of.