Rapid development of JSP applications using JSTL (1)

Source: Internet
Author: User

JSTL is a standardized tag library set. It supports iteration, condition, and XML document parsing, internationalization, and the use of SQL and database interaction functions. At first, the JSTL specification was developed and improved by JSR #52 Under the JCP (Java Community process program) organization. "JCP shoulders the heavy responsibilities of Java Technology Development"-this is the official website's comment. As an open organization, JCP also includes formal and informal members. JCP plays an important leading role in the formation and development of Java technical specifications. JSTL consists of four basic parts: Core, XML, internationalization, and SQL support. This article is mainly about the application of SQL to quickly understand JSTL. Therefore, this article only introduces some basic functions of Core and SQL tag library.

This technology is simple and powerful enough to compete with PHP and ColdFusion. He has sufficient capacity to expand the Java application field. These fields include not only large, resizable Web applications, but also those that only have a simple homepage. This allows you to avoid XML integration and database connection when creating a site. As I mentioned earlier, the key point of JSTL is ease of use. Also, JSTL is built based on JSP. It allows us to use all Java Technologies, which we need to remember.

Before starting, we need to figure out how to run JSTL. Because it is Based on JSP technology, so we need a container that can compile JSP to run it, here we use free JSP Container: TOMCAT (http://jakarta.apache.org/tomcat/index.html ). How to install this product is beyond the scope described in this article. There is no doubt that this software product is now very popular, and there are a lot of documents on how to install it. Assume that you have installed and successfully configured the container. You only need to install the required file for JSTL running. You can download the container from here.

Because we want to run programs on a database that supports standard SQL, you need to install a database on your computer. There are many database types. Here I chose MySQL. The reason why I chose him is that first, we need to demonstrate the role of JSTL in constructing simple and fast application fields, at the same time, it can be compared with PHP + MySQL, which has been dominant in this field. The second point is that MySQL can be downloaded for free and contains a JDBC driver for Java. In short, to use the following example, You need to download a MySQL Server (http://www.mysql.com/products/mysql/index.html;MySQL Connector/j jdbc driver http://www.mysql.com/products/connector-j/index.html# MySQL control center http://www.mysql.com/products/connector-j/index.html ), this product allows you to easily manage MySQL database files. After the download is complete, you need to install MySQL and MySQL Control Center. In addition, the JDBC driver of MySQL needs to be placed in the/web-INF/lib directory of your Web application.

Before creating the program code, you need to create and fill in the database table. The topic articles in this area are also quite extensive, and the specific operations are beyond the scope of this article. Here I recommend you a visual management tool MySQL Control Center mentioned above. You can use it to create a test user, a database, a test table, and a number of records. For environment parameters such as login name, password, and database name, remember them and apply them to our code later.

Now, you can create your first JSTL application. It requires us to do the following:

The whole instance program code includes two files: Hello. jsp and Continue. jsp.

The Hello. jsp file allows you to enter the database name, login name, login password, and database table name. Continue. jsp: accepts the data information in Hello. jsp and connects to the database, and executes a Select request for the table in the database.

The following are all the code files of the system. I will explain them one by one. These codes are quite simple. I believe you can understand the code structure without my explanation.

1: <!-- Hello.jsp -->
2: <html>
3: <head>
4: <title>Hello</title>
5: </head>
6: <body bgcolor="#ffffff">
7: <h1>Please, enter all necessary information and click OK.</h1>
8: <form method="post" action="Continue.jsp">
9: <br>Your login to database:
<input type="text" name="login" size="15">
10: <br>Your password to database:
<input type="password" name="password" size="15">
11: <br>Your database name:
<input type="text" name="database" size="15">
12: <br>Your database table:
<input type="text" name="table" size="15">
13: <br><br><input type="submit" name="submit" value=" OK ">
14: </form>
15: </body>
16: </html>

Note that the numbers on the left side of the text only provide you with some tag information, and you do not need to input them into your code file.

The above is the source code of all Hello. jsp. I'm surprised! It's just pure HTML code. It's so simple. I think there should be no need to comment. The reason why I include these code snippets in my article is to demonstrate how easy it is to integrate JSTL into HTML sites that require rapid expansion of additional features. Let me show you all the code of Continue. jsp. After reading it, you will have some knowledge of JSTL.

1: <!-- Continue.jsp -->
2: <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
3: <@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
4: <c:set var="h" value="localhost"/>
5: <c:set var="l" value="${param.login}"/>
6: <c:set var="p" value="${param.password}"/>
7: <c:set var="d" value="${param.database}"/>
8: <c:set var="t" value="${param.table}"/>
9: <html>
10: <head>
11: <title>Continue</title>
12: </head>
13: <body bgcolor="#ffffff">
14: <sql:setDataSource driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://${l}/${d}?user=${u}&password=${p}"/>
15: <sql:query var="result">
16: SELECT * FROM <c:out value="${t}"/>
17: </sql:query>
18: <c:forEach var="row" items="${result.rowsByIndex}">
19: <c:out value="${row[0]}"/> <br>
20: </c:forEach>
21: </body>
22: </html>

Note that the numbers on the left side of the text only provide you with some tag information, and you do not need to input them into your code file.

This is all our code, isn't it very good? Now let me explain the functions of the above Code.


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.