2004-10-28

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/test/ (or 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/test and http://localhost/newtest, provided you have configured Tomcat to use port 80. It should be remembered that JSTL JAR files (standard.jar and jstl.jar) must be accessible to this application, e.g. must be placed in TOMCAT/webapps/test/WEB-INF/lib.

Consider the first version of this simplistic application:

TOMCAT/webapps/test/index.jspx

<?xml version="1.0" encoding="windows-1256" ?>

<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core">

<jsp:directive.page contentType="text/html; charset=UTF-8" />

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<meta http-equiv="Content-Language" content="fa" />

<title>سلام</title>

</head>

<body style="direction: rtl;">

<h1>سلام</h1>

<c:url scope="page" var="mylink" value="/user/index.jspx">

<c:param name="dummy" value="3" />

</c:url>

<p>

این صفحه‌ی اول پایگاه ما است. برای اطلاعات بیشتر به

<a href="${mylink}">این صفحه</a>

مراجعه کنید.

</p>

</body>

</html>

</jsp:root>

TOMCAT/webapps/test/user/index.jspx

<?xml version="1.0" encoding="windows-1256" ?>

<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:h="urn:jsptagdir:/WEB-INF/tags/html">

<jsp:directive.page contentType="text/html; charset=UTF-8" />

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<meta http-equiv="Content-Language" content="fa" />

<title>اطلاعات</title>

</head>

<body style="direction: rtl;">

<h1>اطلاعات</h1>

<c:url scope="page" var="mylink" value="/index.jspx" />

<p>

مقدار پارامتر داده شده

<c:out value="${param.dummy}" />

است، ولی واقعاً اهمیتی ندارد. حالا می‌توانید به

<a href="${mylink}">صفحه‌ی اول</a>

بر گردید.

</p>

</body>

</html>

</jsp:root>

TOMCAT/webapps/test/WEB-INF/web.xml

<?xml version="1.0" encoding="windows-1256" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<description>Test application to show use of simple tags.</description>

<display-name>TestApp</display-name>

<welcome-file-list>

<welcome-file>index.jspx</welcome-file>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

Now, simple tags can do 2 things for us here:

  1. 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.
  2. 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.

TOMCAT/webapps/newtest/index.jspx

<?xml version="1.0" encoding="windows-1256" ?>

<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:h="urn:jsptagdir:/WEB-INF/tags/html">

<jsp:directive.page contentType="text/html; charset=UTF-8" />

<h:page title="سلام">

<h1>سلام</h1>

<p>

این صفحه‌ی اول پایگاه ما است. برای اطلاعات بیشتر به

<h:a href="/user/index.jspx?dummy=3">این صفحه</h:a>

مراجعه کنید.

</p>

</h:page>

</jsp:root>

TOMCAT/webapps/newtest/user/index.jspx

<?xml version="1.0" encoding="windows-1256" ?>

<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:h="urn:jsptagdir:/WEB-INF/tags/html">

<jsp:directive.page contentType="text/html; charset=UTF-8" />

<h:page title="اطلاعات">

<h1>اطلاعات</h1>

<p>

مقدار پارامتر داده شده

<c:out value="${param.dummy}" />

است، ولی واقعاً اهمیتی ندارد. حالا می‌توانید به

<h:a href="/index.jspx">صفحه‌ی اول</h:a>

بر گردید.

</p>

</h:page>

</jsp:root>

TOMCAT/webapps/newtest/WEB-INF/tags/html/page.tagx

<?xml version="1.0" encoding="windows-1256" ?>

<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core">

<jsp:directive.tag body-content="scriptless" />

<jsp:directive.attribute name="title" required="false" rtexprvalue="true" />

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<meta http-equiv="Content-Language" content="fa" />

<c:if test="${not empty title}">

<title>${title}</title>

</c:if>

</head>

<body style="direction: rtl;">

<jsp:doBody />

</body>

</html>

</jsp:root>

TOMCAT/webapps/newtest/WEB-INF/tags/html/a.tagx

<?xml version="1.0" encoding="windows-1256" ?>

<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core">

<jsp:directive.tag body-content="scriptless" />

<jsp:directive.attribute name="href" required="false" rtexprvalue="true" />

<c:url scope="page" var="mylink" value="${href}" />

<a href="${mylink}">

<jsp:doBody />

</a>

</jsp:root>

TOMCAT/webapps/newtest/WEB-INF/web.xml

<?xml version="1.0" encoding="windows-1256" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<description>Test application to show use of simple tags.</description>

<display-name>TestApp</display-name>

<welcome-file-list>

<welcome-file>index.jspx</welcome-file>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

This was barely more than a "Hello World!" app, but here are two screenshots of the pages to show that it works properly:

TOMCAT/webapps/newtest/index.jspx

 

TOMCAT/webapps/newtest/user/index.jspx

The code archive for this example can be found here.

P.S.

  1. 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.
  2. 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.