Friday, 29 December 2017

Introduction to JSP Standard Template Library (JSTL)



The JSP Standard Template Library (JSTL) is a very new component released by Sun for JSP programming. JSTL allows you to program your JSP pages using tags, rather than the scriptlet code that most JSP programmers are already accustomed to. JSTL can do nearly everything that regular JSP scriptlet code can do. You may be wondering why we need yet another HTML generation programming language.
JSTL was introduced was to allow JSP programmers to program using tags rather than Java code. To show why this is preferable, a quick example is in order. We will examine a very simple JSP page that counts to ten. We will examine this page both as regular scriptlet-based JSP, and then as JSTL.

Installing JSTL

To use JSTL, you must have a JSP 1.2 (or higher) container installed. One of the most commonly used JSP containers is the Apache Tomcat Web server. You can obtain a copy of Tomcat from http://jakarta.apache.org/tomcat/. Using Tomcat alone will allow you to use regular JSP scriptlet code. To use JSTL, you must install JSTL into Tomcat. JSTL can be obtained from the same source as Tomcat. The main URL for JSTL is http://java.sun.com/products/jsp/jstl/. To use JSTL, you must unzip the distribution files and install them into the correct locations within Tomcat.
To properly install JSTL for use with Tomcat, follow these three steps:
 
  1. Copy the JSTL JAR files to Tomcat's lib directory.

    If you are using Windows, the likely location of your lib directory is C:\Program Files\Apache Tomcat 4.0\webapps\ROOT\WEB-INF\lib. There are a number of JAR files included with the JSTL release. You should copy each of these JAR files to the Tomcat JAR directory.


 
  1. Copy the JSTL TLD files to Tomcat's web-inf directory.

    The web-inf directory is likely at this location: C:\Program Files\Apache Tomcat 4.0\webapps\ROOT\WEB-INF. If you examine the JSTL distribution files, you should notice eight files that end with the TLD extension. All eight files should be copied to your web-inf directory.


 
  1. Modify the web.xml file to include the TLD files.

    Finally, you must modify your web.xml, and add an entry for all eight of the tag libraries that you added.

The JSTL Tag Libraries

JSTL is often spoken of as a single-tag library. JSTL is actually four tag libraries. These tag libraries are summarized as follows.
  • Core Tag Library—Contains tags that are essential to nearly any Web application. Examples of core tag libraries include looping, expression evaluation, and basic input and output.

  • Formatting/Internationalization Tag Library—Contains tags that are used to and parse data. Some of these tags will parse data, such as dates, differently based on the current locale.

  • Database Tag Library—Contains tags that can be used to access SQL databases. These tags are normally used only to create prototype programs. This is because most programs will not handle database access directly from JSP pages. Database access should be embedded in EJBs that are accessed by the JSP pages.

  • XML Tag Library—Contains tags that can be used to access XML elements. Because XML is used in many Web applications, XML processing is an important feature of JSTL.
In this article, we will only take a brief look at a few of the core tags. We will examine a simple example that shows how to process data that a user enters into a form. Before we examine this program, we must first see how JSTL handles expressions. Expression handling in JSTL is accomplished by using the EL expression language, just as it is done in JSP 2.0. In the next section, we will examine the EL expression language.

Conclusions

This article showed you some of the differences between the JSTL and standard JSP scriptlet programming. As you can see, JSTL allows a more consistent programming environment by allowing both HTML and procedural code to be expressed as tags. JSTL and tag libraries represent a new method of programming Web pages.
JSTL does not provide everything that a programmer would need to create a full-featured Web application. Further, some procedures that could be programmed in JSTL is often best not programmed in JSTL. One perfect example of this is the database JSTL tags. Except for very small Web applications, it is generally considered bad programming practice to embed actual database commands into a JSP page. The proper location for such program code is in Java beans and EJBs that will be used by your Web application. For such routines, you may consider creating your own tag libraries. This way, your JSP pages can use JSTL to perform basic programming procedures that are not unique to your business. You should implement your own tag libraries to implement components, which are unique to your business, which will be used by your Web application.
JSTL allows you to create a very consistent programming JSP-based application. This is done not just through JSTL, but through a combination of JSTL, your own custom tag libraries, and an underlying database. Understanding each of these components allows you to deploy an effective Web application.

No comments:

Post a Comment