JSTL introduction-New JSP programming components allow tag Programming

Source: Internet
Author: User
Tags tld microsoft frontpage

In JSTL introduction, JSP programmers are allowed to use tags for programming rather than Java code. To illustrate why this is desirable, a quick and simple example is provided. Let's take a very simple example from 1 to 10. We use the conventional scriptlet-based JSP webpage and JSTL to create this webpage. When the number is 10, the example is compiled using scriptlet JSP. the JSP page is displayed 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 above example, the source code of the web page generated by using the scriptlet Code includes a mix of HTML tags and Java statements. There are many reasons why this mixed programming style is not the best.

The main reason for mixed scriptlet and tag-based code is readability. This readability is intended for humans and computers. JSTL allows programmers to view a program consisting of the entire HTML and HTML-like tags.

The readability of JSP scriptlet code is not only applicable to humans. The combination of Scriptlet and HTML code is also hard to be read by computers. Especially HTML production tools such as a Dreamweaver and Microsoft FrontPage. Currently, most HTML production tools use JSP scriptlet code as uneditable blocks and separate them. Generally, HTML production tools do not directly modify the JSP scriptlet code.

The following code shows how to use JSTL to write an example from 1 to 10. As you can see, this code list is more coherent, because only labels are used. Is an example of the combination of HTML and JSTL tags.

< %@ 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 previous code, you can see that the JSP page is composed of tags. The preceding Code uses HTML tags such as

Install JSTL

To use JSTL, you must install a JSP 1.2 (or later) container. The most common JSP Container is Apache Tomcat Web server. You can get a copy of Tomcat from http://jakarta.apache.org/tomcat. Using Tomcat alone allows you to use common JSP scriptlet code. To use JSTL, you must install JSTL to Tomcat. JSTL can be obtained from the same source as Tomcat. The main URL of JSTL is http://java.sun.com/products/jsp/jstl /. To use JSTL, you must decompress the allocated files and install them to the correct location in Tomacat.

To use Tomcat at the same time, follow these three steps to correctly install JSTL:

Copy the jstl jar file to the lib directory of Tomcat.

If you are using Windows, your lib directory may be located in C: \ Program Files \ Apache Tomcat 4.0 \ webapps \ ROOT \ WEB-INF \ lib. Many JAR files are included when JSTL is released. Copy each JAR file to the Tomcat JAR directory.

Copy the jstl tld file to the web-inf directory of Tomcat where the web-inf directory is located: C: \ Program Files \ Apache Tomcat 4.0 \ webapps \ ROOT \ WEB-INF. If you study JSTL file allocation, you should note that the eight extensions are TLD files. These 8 files will be copied to your web-inf directory.

Modify the web. xml file to include the TLD file. Finally, you must modify your web. xml file and add the inbound paths for the eight tag libraries you have added. Add the <taglib> command to the <web-app> command.

< 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 ready to test your JSTL installation. You can use JSTL to create a JSP webpage. The simplest example is the "count to ten" example above. You should put your JSP file in the Webroot directory (C: \ Program Files \ Apache Tomcat 4.0 \ webapps \ ROOT ). Once the Tomcat server is started, you can view your webpage through http: // 127.0.0.1: 8080/count. jsp.

If you have not correctly installed JSTL, there may be no error message. If JSTL cannot translate your tags, they are directly sent to a web browser. Web browsers translate these tags as unknown HTML tags. Most browsers ignore unknown HTML tags.

JSTL tag Library

JSTL is often used as a separate tag library. In fact, JSTL has four tag libraries. These tag libraries are summarized as follows:

Core Tag Library-the included tags are essential to Web applications. Examples of core tag libraries include loops, formula evaluations, and basic input and output.

Formatting/Internationalization Tag Library-the included tags are used to parse the code. Some labels parse code, such as date, based on the current region.

Database Tag Library-the included tags are used to access the SQL Database. These labels are generally used only to create prototype programs. This is because most programs do not directly process database access from JSP web pages. Database Access should be placed in EJBs and accessed through JSP web pages.

XML Tag Library-the included tags can be used to access XML elements. Because XML is used in many Web applications, XML processing is an important function of JSTL.

In this article, we only need to look at a few core tags (core tags ). A simple example is provided to show how users process data when entering a table. Before proceeding to this program, we should first take a look at JSTL handles expressions. Expression Processing in JSTL is done by using the EL expression language, just like in JSP2.0. In the next section, we will check the EL Expression Language.

EL Expression Language

One of the main components of JSP2.0 is EL's new expression language. EL is widely used in JSTL. However, it is important to remember that EL is a feature of JSP rather than JSTL. The JSP scriptlet code used with JSP2.0 includes EL expressions. The following example uses EL in JSP scriptlet code.

       < p> 
Your total, including shipping is ${total+shipping}

< /p>

As you can see in the above Code, the "total" and "shipping" values are added and generated as HTML display. These expressions can also be used in JSTL labels. An important requirement of JSTL1.0 is that JSTL should be used with JSP1.2. Because JSP1.2 does not support EL, it is necessary to provide some additional JSTL labels to use EL. For example, if you want to use JSTL to display the above expressions, you can use the following code:

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

< /p>

JSP2.0 is not required to run JSTL. This requirement can be met only by providing a label that can display EL expressions.

JSTL example

Now let's look at a simple example of using JSTL. In this example, we will look at a common program that many Web applications can do. We will see how to POST a table and run the results from it. A simple program that can be done in this way is shown 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 of?

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

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

< br />

< /form>

< /body>

< /html>

This simple Web page will display a table and ask the user to guess which computer language can be considered by the program. Of course, the computer will consider "Java .". First, check whether a POST is complete. This allows the table and the Code for processing the table to be placed on a single page. This is done through the following JSTL if statement.

< c:if test="${pageContext.request.method=='POST'}">

Here you can see that the <c: if> label uses an EL expression to evaluate whether the requested method is a POST. If the data is pasted on a webpage, the user enters their guess value and stores it in a parameter named "guess. This is because "guess" is specified as the name of the entry in the table. Check whether this parameter is "Java ". This is done using the <c: if> label below.

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

< /c:if>

As you can see, if the statement evaluation is correct, the subject of the <c: if> label is executed. In this article, we begin to study the basic knowledge about how to install JSTL and how it works.

JSTL core labels also include loops, repetition, and variable processing labels. By using these tags, you can repeat these sets to access user conversation data and execute other core tasks executed by all Web applications. In addition to the core tag library, XML, database, and formatting tag library, it also provides more advanced functions.

Summary

This article shows you some differences between JSTL and JSP scriptlet programming. As you can see, JSTL recognizes a more consistent programming environment, expressed by recognizing HTML and program code as tags together. JSTL and tag library represent a new method for Web programming.

JSTL does not provide everything that a programmer needs to create a fully functional web application. In addition, some programs that can be programmed in JSTL are usually better not included in JSTL. The best example of this is the database JSTL tag. In addition to every small Web application, it is generally considered a bad programming habit to embed actual database commands into a JSP page. The proper place for such program code is in the Java beans and EJBs that your Web applications often use. For this practice, you can consider creating your own tag library. In this way, you can use JSTL to execute not just your business programming programs. You should complete your own tag library to execute components. These are unique to your business and are also used in your Web applications.

JSTL allows you to create a very consistent programming JSP technology-based application. This is not done through JSTL, but through JSTL, your own custom tag library and a basic database combination. Understanding each of these components allows you to deploy a more effective Web application.

What you are reading is "JSTL introduction-New JSP programming components allow tag programming"

  1. JSP best practices use JSTL to update JSP pages
  2. Advantages and implementation of jstl and el jsp page development
  3. JSTL enables rapid development of JSP applications

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.