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 very 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 run it requires a container that can compile JSP, 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. If you have installed and successfully configured the container, you only need to install the jstl file to run it. 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. 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 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: 4: <title> Hello </title>
5: 6: <body bgcolor = "# ffffff">
7: 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> <input type = "Submit" name = "Submit" value = "OK">
14: </form>
15: </body>
16:

(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 it is to integrate jstl 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: 11: <title> continue </title>
12: 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:

(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.

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. 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) to establish a database connection. In the URL, we specify the parameters required for database connection, such as the database name, host 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 be directly executed 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 results. <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}. Then, in row 19th, we use <C: 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 of this information can be found in the jstl or Tomcat manual. If you want to understand how they are running and why they are running, 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 database-based front-end and back-end applications.

Related Article

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.