Jstl is a standardized collection of tag libraries that supports iterations, conditions, parsing of XML documents, internationalization, and the ability to interact with the database using SQL. Jstl mainly includes four basic parts of the tag library: Core,xml, internationalization, and support for SQL. Since this article is primarily based on the SQL part of the application to quickly understand jstl, so our article only describes the core and SQL Tag library Some of the basic features.
The technology is simple and powerful enough to be high with PHP and ColdFusion. He has the ability to expand Java applications, not just for large, scalable Web applications, but for Web programs with simple home pages. This allows you to avoid the integration of XML that is often considered and the connection to the database when you build the site. The key point of jstl is simplicity and ease of use. Also, that is, Jstl is built on JSP, and he allows us to use all the Java technologies.
Before we start, we need to figure out how to run Jstl. Because it's based on JSP technology, we need a container that compiles the JSP, where 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 widely available, and there are many documentation on how to install it. This assumes that you have installed and successfully configured this container, you only need to install JSTL to run the required files it can download from here http://jakarta.apache.org/taglibs/doc/standard-doc/ Intro.html you don't have to really install it, just include the. jar file in your application's Web-inf/lib directory.
Because we're running a program on a database that supports standard SQL, you need to have a database installed on your computer. There are many types of databases, and I chose MySQL here. The reason why I chose him is that first, we need to demonstrate JSTL's role in constructing a simple and fast application domain, while comparing it to the Php+mysql, which has been in the dominant position in this field. The 2nd is that MySQL is free to download 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 and MySQL control center http://www.mysql.com/products/connector-j/index.html), this product can make it very easy for you to manipulate MySQL database files. After all the downloads are complete, you will need to install MySQL and MySQL control Center. There is also the MySQL JDBC driver that 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 table. The topic of this article is also quite extensive, how the specific operation is beyond the scope of this article. Here I recommend a visual management tool that we mentioned above MySQL control Center. You can use him to create a test user to run a program, a database, and create a test table and fill out a few records. About login name and password database name these configuration environment parameters, you should remember them, later need to apply to our code.
Now you're ready to create your own first JSTL application. It requires us to do some of the following things:
The entire instance program code consists of two files hello.jsp and continue.jsp
hello.jsp file allows you to enter the database name, login name, login password, database table name. CONTINUE.JSP: Accepts the data information from the hello.jsp and connects to the database and executes a select request to the tables in the database.
Here are all the code files for this system, and I'll explain each one. The code is quite simple, and for the structure of the code, I believe you can understand even without my explanation.
1: The!--hello.jsp-->
2:
3:
4: Hello
5:
6:
7: Please
, enter all necessary information and click OK.
8:
9:
Your Login to database:
Your Password to database:
One:
Your database name:
Your database table:
:
:
:
:
(Note that the numbers on the left side of the text just give you some tag information and you don't need to enter them into your code file.) )
It's all hello.jsp source code, and it's amazing, he is just pure HTML code, so simple, I think there should be no annotation necessary. I've included these snippets in the article to show how easy it is to integrate JSTL into HTML sites that need to quickly augment additional functionality. Let me be You show continue.jsp all the code, and after you read it, you'll have some idea of jstl.
1: The!--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:
5:
6:
7:
8:
9:
One: Continue
Url= "Jdbc:mysql://${l}/${d}?user=${u}&password=${p}"/>
:
16:select * from
:
:
:
(Note that the numbers on the left side of the text just give you some tag information and you don't need to enter them into your code file.) )
This is all of our code, isn't it pretty good? Now let me explain what the above code does.
Line 1 is an annotated description of HTML.
Line 2--3 These JSP tags are intended to refer to an external tag library, or rather, to refer to the Core and SQL tag Library sections in the Jstl library. We have set the prefix names for them so that we can access the functional methods in the tag library that are introduced through these prefix names.
Row 4---8 as hello.jsp really works, he will request continue.jsp,continue.jsp to get and parse a number of variables from hello.jsp, and we have used ${param in this way. Your_var}. In line 4th
Line 9--13 is a simple HTML tag that I often use for common HTML Web headers, and immediately, important features are coming up.
Line 14, we tried to use our previous MySQL driver (com.mysql.jdbc.Driver) to establish a database connection, in the URL, we indicate the database connection required parameters, such as database name, host name, login name and login password. In this way, we can use any other JDBC driver to connect to its relative database. If we need to connect to another SQL database, just change the URL.
Row 15--17 Here we execute a select query, please pay special attention to line 16th, we use another jstl function
Line 18--20 Now that we have executed the select query, we should show him the result of his return. displays the values in the first data column of each data row returned. As long as you have fields in your datasheet, you can access the values in any field in the request table by changing the size of the numbers in the variable ${row}.
Row 21--22 is an HTML footer
In the process of creating your own JSTL application, you may not have discovered how powerful it is, but you should be aware of the simplicity and efficiency of JSTL's functional functions, and imagine how fast it would be to integrate an sql-based news column with Jstl, and how easy it is to integrate your existing Web site.
Well, our code is easy to understand, even an unprofessional programmer, for example, even a designer can read it, understand it, and possibly make some changes, at least the layout of the page.
As we mentioned at the outset, in order for our JSTL code to run properly, we need to install the jar file in the MySQL connector/j, and of course, JSTL. Because we use Tomcat's JSP container, you need to create your own folder under Tomcat's file directory WebApps your hello.jsp, continue.jsp files in the directory you created, in the directory you created you also want to create a folder called Web-inf, put your profile web.xml inside, web.xml files are as follows:
! DOCTYPE Web-app
Public "-//sun Microsystems, INC.//DTD Web application 2.3//en"
"Http://java.sun.com/dtd/web-app_2_3.dtd" >
Next we need to create a subdirectory called Lib under 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)