Introduction
JSP Standard Template Library (JSTL) is a new component developed for JSP released by SUN. JSTL allows you to develop JSP pages using tags instead of the scriptlet code that most JSP programmers are used to. JSTL can do almost anything that traditional JSP scriptlet code can do. You may be wondering, why do we need another such HTML generation language?
STL allows JSP programmers to program using tags instead of JAVA code. To show why this is superior, an example will be given below. We'll examine a very simple JSP page that counts from 1 to 10. We will check through two methods, one is based on JSP scriptlet and the other is JSTL. When this counter page example is written using a JSP scriptlet, the JSP page looks like this:
<html>
<head>
<title>Count to 10 in JSP scriptlet</title>
</head>
<body>
<%
for(int i=1;i<=10;i++)
{%>
<%=i%><br/>
<%
}
%>
</body>
</html>
As you can see in the above example, the page source code generated using scriptlet code will contain a mixture of HTML tags and JAVA statements. This hybrid programming approach is not the best approach for the following reasons.
The main reason is its readability. This readability is mainly dependent on humans and computers. JSTL allows programmers to view a page that contains only complete HTML and HTML-like tags.
SP scriptlet code is not readable for humans. This mix of scriptlets and HTML code is also difficult for computers to read. Especially for those official HTML tools such as Dreamweaver and Microsoft FrontPage, the unintuitiveness is even more prominent. Currently, most official HTML tools isolate JSP scriptlet code in the form of non-editable blocks. This kind of official HTML tool usually cannot directly modify the JSP scriptlet code.
The following code shows how this counter example can be written using JSTL. As you can see, this code listing has immutability, only one tag is used. HTML and JSTL tags are mixed to produce this program.
<%@ taglib uri=" http://java.sun.com/jstl/core " prefix="c" %>
<html>
<head>
<title>Count to 10 Example (using JSTL)</title>
</head>
<body>
<c:forEach var="i" begin="1" end="10" step="1">
<c:out value="${i}" />
<br />
</c:forEach>
</body>
</html>
When you examine the code for the above example, you will see that the JSP page only contains tags. The above code uses HTML tags such as <head> and <br>. This tag usage is not limited to HTML tags. This code can also use JSTL tags such as <c:forEach> and <c:out>. In this article, some basics of JSTL will be introduced.
Install JSTL
To use JSTL, you must have a JSP 1.2 (or higher) container installed. The most common JSP container is Apache Tomcat. You can download it from http://jakarta.apache.org/tomcat/ . Standalone Tomcat allows you to use regular JSP scriptlet code. To use JSTL, you must install JSTL in Tomcat. The main URL for JSTL is http://java.sun.com/products/jsp/jstl/ . To use JSTL, you must unzip this file and install it into the correct location for Tomcat.
To install JSTL in Tomcat, there are three steps:
Copy the JSTL JAR file to Tomcat's lib directory.
If you are using Windows, the most likely location of the lib directory is C:Program FilesApache Tomcat 4.0webapps ROOTWEB-INFlib. You should copy these JAR packages to your Tomcat JAR directory.
Copy the JSTL TLD files to Tomcat's web-inf directory.
When you examine the JSTL distribution files, you should notice that 8 files end with the TLD extension. All 8 files should be copied to your web-inf directory.
Modify the web.xml file to include these TLD files.
Finally, you must modify your web.xml and add 8 tag library entries. The table items that need to be added are as follows:
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
<taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
<taglib-location>/WEB-INF/c-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
<taglib-location>/WEB-INF/sql.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
<taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
<taglib-location>/WEB-INF/x.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
<taglib-location>/WEB-INF/x-rt.tld</taglib-location>
</taglib>
After completing the above three steps, you are now ready to test your JSTL installation. This can be verified by creating a JSP page containing JSTL. One of the simplest example programs is the counter program above. You should place it in your webroot directory (C:Program FilesApache Tomcat 5.0webappsROOT). Once Tomcat starts, you should be able to view this page by browsing to http://127.0.0.1:8080/count.jsp .
If you did not install JSTL correctly, the error message may not appear. If JSTL cannot interpret the meaning of your tags, it will be skipped by the web browser. The web browser will then interpret these unknown HTML tags. Most browsers simply ignore these unknown HTML tags.
To be continued...
Translated by Caiyi0903(Willpower),2004.2.19