Introduction to JSP standard tag Library

Source: Internet
Author: User
Tags tld microsoft frontpage

 

Introduction

The JSP standard template library (JSTL) is a new component developed for JSP by SUN. JSTL allows you to use tags for JSP page development, rather than using scriptlet code that most JSP programmers are used. JSTL can do almost anything that traditional JSP scriptlet code can do. You may wonder why we need another HTML language?

 

STL allows JSP programmers to use tags instead of JAVA code for programming. The following example shows why this is superior. We will check a very simple JSP page from 1 to 10. We will check through two methods: one is JSP-based scriptlet and the other is JSTL. When the example of this counter page is written using JSP scriptlet, the JSP page is as follows:

 

<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 preceding example, the page source code generated using the scriptlet code contains a mix of HTML tags and JAVA statements. This hybrid programming method is not the best, mainly because of the following reasons.

 

The main reason is its readability. This readability mainly depends on humans and computers. JSTL allows programmers to view a page that only contains complete HTML and HTML-like tags.

 

The readability of SP scriptlet code is not suitable for humans. This kind of mixed scriptlet and HTML code is also hard for computers to read. Especially for official HTML tools such as Dreamweaver and Microsoft FrontPage, these tools are more intuitive. Currently, most official HTML tools isolate JSP scriptlet code in the form of non-editable blocks. This official HTML tool cannot directly modify the JSP scriptlet code.

 

The following code shows how to use JSTL to compile this counter example. As you can see, this code list is immutable and only one label is used. HTML and JSTL labels are mixed to generate 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 check the code in the above example, you will see that the JSP page only contains tags. The above Code uses HTML tags such as

 

Install JSTL

To use JSTL, you must install a JSP1.2 (or later) container. The most common JSP Container is Apache Tomcat. You can download it from http://jakarta.apache.org/tomcat. Independent Tomcat allows you to use common JSP scriptlet code. To use JSTL, you must install JSTL in Tomcat. The main URL of JSTL is http://java.sun.com/products/jsp/jstl /. To use JSTL, you must decompress the file and install it to the correct location of Tomcat.

 

To install JSTL in Tomcat, follow these three steps:

 

Copy the jstl jar file to the Tomcat lib directory.

 

If you are using Windows, the most likely location of the lib directory is C: \ Program Files \ Apache Tomcat 4.0 \ webapps \ ROOT \ WEB-INF \ lib. Copy these JAR packages to your Tomcat JAR directory.

 

Copy the jstl tld file to the Tomcat web-inf directory.

 

When you check the JSTL publishing file, you should note that eight files end with the TLD extension. All these 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 the table items (entries) of the eight tag libraries ). The table items 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 can now prepare to test your JSTL installation. You can create a JSP page containing JSTL for verification. The simplest example program is the counter program above. You should place it in your Webroot directory (C: \ Program Files \ Apache Tomcat 5.0 \ webapps \ ROOT ). Once Tomcat is started, you should be able to view this page through http: // 127.0.0.1: 8080/count. jsp.

 

If you have not correctly installed JSTL, no error message may appear. If JSTL cannot explain the meaning of your tag, it will be skipped on the Web browser. The Web browser then explains these unknown HTML tags. Most browsers only ignore these unknown HTML tags.

 

JSTL tag Library

JSTL is generally considered a single-tag library. However, JSTL actually has four tag libraries. These tag libraries are summarized as follows:

 

Core tag library-contains the necessary tags for some Web applications. Examples of the core tag Library include loops, expression assignment, and basic input and output ..

 

 

Format/International tag library-including the tag library used to parse data. This label will parse the data, such as the date and region.

 

 

Database tag library-contains tags used to access the SQL database. These labels generally only create prototype programs. This is because most programs do not process database access directly from JSP pages. Database Access should be embedded in ejbs that can be accessed by JSP pages.

 

 

XML tag library-contains tags used to access XML elements. Because XML is widely used in Web development, XML processing is an important feature of JSTL.

In this article, we will only give a brief introduction to core labels. We will look at a simple example to illustrate how to process the data entered by the user in the form. Before we begin to check the program, we must first look at how JSTL processes expressions. JSTL expressions are processed by using the EL expression language and can only be used in JSP2.0. In the next section, we will examine the EL Expression Language.

 

EL Expression Language

A major component of JSP2.0 is the new expression language called EL. EL is widely used in JSTL. However, it is important to remember that EL is a JSP function rather than a JSTL. The JSP scriptlet code used in JSP2.0 can contain EL expressions. The following code demonstrates EL in JSP scriptlet code:

 

<P>

Your total, including shipping is $ {total + shipping}

</P>

 

As you can see, total and shipping will generate HTML and display their values in the browser. These expressions can also be used in JSTL labels. An important issue of JSTL1.0 is that JSTL cannot be used in JSP1.2. Because JSP1.2 does not support EL, it is necessary to provide some additional JSTL labels to help EL use. For example, if you want to use JSTL to display the above expression, you can use the following code.

 

<P>

Your total, including shipping is <c: out var = "$ {total + shipping"/>

</P>

 

It is worth noting that JSTL does not need JSP2.0 to run. You can achieve this by providing a label that can display EL expressions.

 

JSTL example

We will examine a simple example of using JSTL. In this example, we will examine a common process in Web applications. We will see how to submit (POST) A form and process the POST result. The program code is as follows:

 

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

<Html>

<Head>

<Title> If with Body </title>

</Head>

 

<Body>

<C: if test = "$ {pageContext. request. method = 'post'}">

<C: if test = "$ {param. guess = 'java'}"> You guessed it!

<Br/>

 

<Br/>

 

<Br/>

</C: if>

 

<C: if test = "$ {param. guess! = 'Java'} "> You are wrong

<Br/>

 

<Br/>

 

<Br/>

</C: if>

</C: if>

 

<Form method = "post"> Guess what computer language

I am thinking?

<Input type = "text" name = "guess"/>

 

<Input type = "submit" value = "Try! "/>

 

<Br/>

</Form>

</Body>

</Html>

 

This simple Web page displays a form that requires the user to guess the computer language that the program thinks. Of course, the computer is thinking of "Java ". This page starts by checking "submitted. This allows the form and the Code for processing the form to be placed on the same page. It is implemented through the following JSTL if statement.

 

<C: if test = "$ {pageContext. request. method = 'post'}">

 

Now you can see that the <c: if> label uses the EL expression to determine whether the request method is POST. If the data is post to the page, the user-input conjecture values will be stored in the parameter "guest. This is because "guest" is the name of the input form. Now we must check whether this parameter is equal to "Java ". This is done through the <c: if> label below:

 

<C: if test = "$ {param. guess = 'java'}">

You guessed it!

</C: if>

 

 

As you can see, if the expression is true, the TAG body of the <c: if> label is executed. In this article, we begin to examine how JSTL is installed and how it runs. We also provide some small examples of JSTL to help you understand.

 

JSTL core labels also contain loops, stacks, and variable processing. By using tags, you can perform iterate in collections, Access session data, and execute other core tasks. In addition to the core tag library, XML, database, and formatted tag library, it also provides more advanced functions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.