Rapid development of JSP applications using JSTL

Source: Internet
Author: User

In this article, I will introduce you to a JSP-based and flexible and interesting technology, that is, JSTL. JSTL is called Java Server Pages Standard Tag Library. although JSP has become increasingly popular, JSTL has not been widely used in simple and fast front-end and back-end development based on SQL databases. Once you understand JSTL, you will understand its advantages. Concurrency has been applied to many aspects in your daily work as a programmer. Let me assume that you are familiar with HTML, basic SQL statements, and basic Jsp knowledge. Because the following content involves this knowledge.

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 shoulder the heavy responsibilities of Java Technology Development"-this is the official website's comment. As an open organization, JCP also absorbs formal members 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 JSTL file to run the container. You can download the Installation tool from. the JAR file is included in the WEB-INF/lib directory of your application. I will talk about how to do it later.

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 Connector is the 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 and password database name, you should remember them and apply them to our code later.

Now, you can prepare to 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: accept the data information in Hello. jsp and connect to the database, and execute 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. The code is quite simple. For the code structure, I believe you can understand it 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 enter them into your code file .)

Above is all Hello. the source code of jsp is amazing. It is just pure HTML code. It is so simple. I think there should be no need to comment. the reason why I include these code snippets in my article is to show how simple JSTL is to integrate them into HTML sites that require rapid expansion of additional features. let me show you more about Continue. after reading all the jsp code, you will have some knowledge about 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 enter 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 Generation.

Line 1 is the description of HTML annotations.

Line 2-3 JSP labels are used to reference external tag libraries. More specifically, the Core and SQL tag libraries in the JSTL library are referenced. We have set prefix names for them so that we can use these prefix names to access the functional methods in the introduced tag library.

Line 4---8 is like Hello. jsp is actually running, and he will request continue. jsp, Continue. after obtaining the request, jsp needs to get and parse the request from Hello. several jsp variables, we use this method $ {param. YOUR_VAR }. In the <c: set label of row 4th, set the variable $ {h} to "localhost". The fifth line variable $ {l} will get the variable in Hello. the information entered in the login text field in jsp. the variables in rows 6, 7, and 8 will get the information from Hello. the password, database name, and data table name entered by the user in jsp.

Line 9-13 is some simple HTML tags that I often use for common HTML webpage headers. Now, important functions are coming soon.

Line 14: we tried to use the mysql Driver (com. mysql. jdbc. driver) Create a database connection. In the URL, we specify the parameters required for database connection, such as the database name, Master name, login name, and login password. Therefore, we can use any other JDBC driver to connect to the database. To connect to other SQL databases, you only need to change the URL.

Row 15--17 here we run a Select query. Please pay special attention to row 16th. We use another JSTL function <c: out to output the name of the data table we have obtained, here we can also use other SQL commands, such as INSERT, DELETE, and so on. To execute these query requests without return values, you need to use the <SQL: update JSTL function. They can directly execute them, just like <SQL: query, but they do not need to specify the result variable to store the returned results of statement execution.

Line 18--20 since we have executed the preceding SELECT query statement, we should display its return result. <C: forEach is a function with iteration function in JSTL. When executing this function, we use $ {result. rowsByIndex} returns the information of each returned data row to the variable $ {row}, followed by row 19th: out value = "$ {row [0]}"/> displays the value in the first data column of each returned data row. As long as your data table contains fields, you can change the numeric size in the variable $ {row} to access the values of any field in the request table.

Line 21-22 is the HTML footer

In the process of creating a JSTL application, you may not have discovered how powerful it is, but you should be able to realize the simplicity and efficiency of JSTL function, how fast is it to integrate an SQL-based news column and how easy it is to integrate your existing web site.

Well, our code is easy to understand. Even a non-professional programmer, for example, a designer can understand it, understand it, and make some modifications, at least modification is made on the page layout.

As we mentioned at the beginning, to make our JSTL code run normally, we need to install the JAR file in Mysql Connector/J, and of course JSTL. Because we use the JSp Container Tomcat, you need to create your own folder under the Tomcat file directory Webapps, and set your Hello. jsp and Continue. jsp files are placed in the file directory you created, in the directory you created, you also create a folder called WEB-INF, your configuration file Web. put xml in it, web. the xml file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app />

Next we also need to create a project named lib under the WEB-INF and put the following files in:

jstl.jar

saxpath.jar

standard.jar

Mysql-connector-java-3.0.9-stable-bin.jar (note that this name may vary depending on your Mysql Connector/J Version)

All this information can be found in the JSTL or Tomcat manual. If you want to understand how their bodies run and why they run so much, you should read these manuals. However, to help you quickly master the basic operations of JSTL, I have already introduced relevant knowledge.

If you are using other Jsp containers, you need to read their related manuals.

Luo sosuo is so many that I would like to explain a little more. This article is only a basic introduction to JSTL technology and is not a complete manual. JSTL contains many functions to help you complete Jsp development in a simple and fast manner, I suggest you read more detailed documents about the JSTL function and how it works with JavaBeans. In the end, you may find that it is your long-awaited development platform. By reading this article, you should be able to create some simple SQL-based front and back-end 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.