In this article, I will introduce you to a flexible and interesting technology based on JSP, which is JSTL. JSTL stands for Java Server Pages Standard Tag Library. Although JSP has become very popular, JSTL is still very popular in SQL database-based applications. Simple and fast front-end and back-end development is still not widely used. Once you understand JSTL, you will understand its advantages and find that it has been used in many aspects in your daily work as a programmer. I assume here that you are already familiar with HTML, can understand basic SQL statements, and the basics of JSP. Because the following content involves this knowledge.
JSTL is a collection of standardized tag libraries that support iteration, conditions, XML document parsing, internationalization, and the ability to interact with databases using SQL. Initially, the JSTL specification has been developed and improved by JSR #52 organized by JCP (Java Community process program). "JCP shoulders the important task of Java technology development" - the official website comments. As an open organization, JCP absorbs both formal members and informal members. JCP has played an important leading role in the formation and development of Java technical specifications. JSTL mainly includes four basic parts of the tag library: Core, XML, internationalization, and support for SQL. Since the main purpose of this article is to quickly understand JSTL through the application of the SQL part, this article only introduces some basic functions of Core and SQl tag libraries.
This technology is simple and powerful enough to compete with PHP and ColdFusion. It has enough ability to expand the application fields of Java. These fields include not only large-scale and scalable Web applications, but also those with simple homepages. Web programs are no problem. This allows you to avoid the usual considerations of XML integration and database connections when building your site. As I just mentioned, the key point of JSTL is simplicity and ease of use. Also, that is, JSTL is built on JSP, which allows us to use all Java technologies, which we need to remember.
Before we start, we need to figure out how to run JSTL. Since it is based on JSP technology, we need a container that can compile JSP to run it. Here we use a free JSP container: TOMCAT ( http://jakarta.apache. org/tomcat/index.html ). How to install this product is beyond the scope of this article. There is no doubt that this software product is now very popular, and there is a lot of documentation on how to install it. It is assumed here that you have installed and successfully configured this container. You only need to install the files required to run JSTL. It can be downloaded from http://jakarta.apache.org/taglibs/doc/standard-doc/intro. html, you don’t need to actually install it, you just need to include the .JAR file in the WEB-INF/lib directory of your application. I will explain how to do it later.
Because we are going to be in a To run programs on a database that supports standard SQL, you need to have a database installed on your computer. There are many types of databases, here I chose MySql. The reason why I chose him is that first of all, we need to show the role of JSTL in constructing simple and fast application fields, and at the same time, it can be compared with PHP+MySql, which has always been dominant in this field; the second point is MySql is free to download and includes a JDBC driver for JAVA. In short, in order to use the following examples, 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 ; and MySql control center http://www.mysql.com/products/connector-j/index.html ), this product allows you to operate and manage Mysql database files very easily. After all downloads are completed, you need to install mysql and mysql Control Center. In addition, the JDBC driver for mysql needs to be placed in the /Web-INF/lib directory in your web application.
Before creating the program code, you need to create and fill in the database tables. There are extensive articles on this topic, and how to do it is beyond the scope of this article. Here I recommend to you MySQL Control Center, a visual management tool we mentioned above. You can use it to create a test user for running the program, database and create a test table and fill in a number of records. Regarding the configuration environment parameters such as login name and password database name, you should remember them and need to apply them to our code later.
Now, you are ready to create your first JSTL application. It requires us to do the following things:
The entire example 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, database table name. Continue.jsp: Accepts the data information in Hello.jsp and connects to the database, and performs a Select request to the table in the database.
Below are all the code files of this system, I will explain them one by one. These codes are quite simple. As for the structure of the code, I believe you can understand it even 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>
(Please note that the numbers on the left side of the text are just to provide you with some markup information. You do not need to enter them into your code file.)
The above is the source code of all Hello.jsp. Surprisingly, It is just pure HTML code, it is that simple, I think there is no need for comments. The reason why I include these code snippets in the article is to demonstrate the integration of JSTL into HTML sites that need to quickly expand additional functions. How simple. Let me show you the entire code of Continue.jsp. After reading it, you will have some understanding 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>
(Please note that the numbers to the left of the text are just to provide you with some markup information, you do not need to enter them into your code file.)
This is all our code, isn't it great? Now let us explain the function of the above codes.
Line 1 is the HTML comment description.
Lines 2--3 These JSP tags are used to reference external tag libraries. To be more precise, they refer to the Core and SQL tag library parts of the JSTL library. We set prefix names for them so that we can access the function methods in the introduced tag library through these prefix names.
Line 4---8 Just like Hello.jsp is actually running, it will request continue.jsp. After Continue.jsp gets the request, it needs to get and parse several variables from Hello.jsp. We use this method ${param .YOUR_VAR}. In the 4th line <c:set tag, set the variable ${h} to "localhost". The fifth line variable ${l} will get the information we entered in the login text field in Hello.jsp, 6th and 7th , the variables in the 8 lines will respectively obtain the password, database name, and data table name entered by the user in Hello.jsp.
Lines 9-13 are some simple HTML tags that I often use to create HTML page headers. Now, the important functionality is coming.
Line 14, we try to establish a database connection using the mysql driver (com.mysql.jdbc.Driver) we obtained earlier. In the URL, we specify the parameters required for the database connection, such as database name, host name, login name and login password. Accordingly, we can use any other JDBC driver to connect to its corresponding database. If we need to connect to other SQL databases, just change this URL.
Lines 15--17 Here we execute a Select query. Please pay special attention to line 16. We use another JSTL function <c:out to output the data table name we obtained. Here we can also use other SQL commands, such as INSERT, DELETE, etc. To execute these query requests without return values, you need to use the <sql:update JSTL function. He can execute them directly just like <SQL:query, except that when executing it, there is no need to specify a result variable to store the results returned by the statement execution.
Lines 18--20 Now that we have executed the above SELECT query statement, we should display its return results. <c:forEach is a function with iteration function in JSTL. When executing it, we return each data row information returned to the variable ${row} through ${result.rowsByIndex}, and then on line 19, we Use <c:out value="${row[0]}"/> to display the value in the first data column of each returned data row. As long as your data table contains fields, you can access the value in any field in the request table by changing the number in the variable ${row}.
Lines 21-22 are HTML footers.
In the process of creating JSTL applications yourself, you may not have discovered how powerful it is, but you should be able to realize the simplicity and efficiency of JSTL functions. Just imagine having JSTL , how fast it is to integrate a SQL-based news column, and how easy it is to integrate your existing web site.
Very good, our code is very easy to understand. Even a non-professional programmer, for example, a designer can read it, understand it, and possibly make some modifications, at least to the page layout. .
As we mentioned at the beginning, in order for our JSTL code to run properly, we need to install the JAR files in Mysql Connector/J, and of course JSTL. Because we use Tomcat, the JSP container, you need to create your own folder under Tomcat's file directory Webapps, and place your Hello.jsp and Continue.jsp files in the file directory you created. You also need to create a folder called WEB-INF and put your configuration file Web.xml in it. The web.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 need to create a subdirectory called lib under WEB-INF and put the following files into it:
jstl.jar
saxpath.jar
standard.jar
mysql-connector-java-3.0.9-stable-bin.jar (note, this name may change depending on your Mysql Connector/J version)
All this information you can check in the JSTL or Tomcat manual , if you want to understand exactly how and why they work you should read these manuals. However, in order to help you quickly master the basic operations of JSTL, I have introduced the relevant knowledge.
If you are using other Jsp containers, you need to read their relevant manuals.
So much, I would like to explain one more thing. This article is only a basic introduction to JSTL technology, not a complete manual. JSTL contains many feature-rich function usages to help you complete your Jsp development in a simple and fast way. I recommend that you read some more detailed documentation about JSTL functions and how it works with JavaBeans. In the end, you may You will find that it is the development platform you have been waiting for. By reading this article, you should be able to create some simple front-end and back-end applications based on SQL databases.