Using JSTL to realize rapid development of JSP application

Source: Internet
Author: User
Tags foreach contains connect mysql php and table name variable mysql database
js| program in this article, I will introduce 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, but jstl in the SQL database based on the simple, rapid development of the front and back platform is not widely used. Once you get to know Jstl, you will learn about its advantages and find that it has been applied to many aspects of your day-to-day work as a programmer. I assume that you are familiar with HTML, understand basic SQL statements, and the basics of JSP. Because the following content involves this knowledge.

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. At first, the JSTL specification has been developed by the JSR #52, organized by JCP (Java Community process program), "JCP shoulder the brunt of the development of Java Technology"-as the official web site evaluates. JCP as an open organization, he also absorbs both formal 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 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. As I mentioned earlier, the key point of jstl is simplicity and ease of use. And that is, Jstl is built on JSP, and he allows us to use all the Java technology, which we need to remember.

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, and I'll talk specifically about what to do later.

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: <title> Hello </title>
5: 6: <body bgcolor= "#ffffff"
7: Please 8: <form method= "POST" action= "continue.jsp"
9: <br> Your Login to database:
<input type= "text" name= "login" size= "15"
<br> Your Password to database:
<input type= "password" name= "password" size= "15"
One: <br> Your database name:
<input type= "text" name= "database" size= "15"
<br> Your database table:
<input type= "text" name= "table" size= "15"
: <br> <br> <input type= "Submit" name= "Submit" value= "OK"
: </form>
: </body>
:
(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: <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: One: <title> Continue </title>
<body bgcolor= "#ffffff"
<sql:setdatasource driver= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://${l}/${d}?user=${u}&password=${p}"/>
: <sql:query var= "Result" >
16:select * from <c:out value= "${t}"/>
: </sql:query>
<c:foreach var= "Row" items= "${result.rowsbyindex}"
<c:out value= "${row[0]}"/> <br>
</c:forEach>
: </body>
:
(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 <c:set the label, set the variable ${h} to "localhost", and the fifth row variable ${l} will get our Hello.jsp the information entered in the Login text field in the 6,7,8, the variable in the row will get the password, database name, and data table name entered from the user in hello.jsp.

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 <c:out to output the name of the data table we obtained, we can also use other SQL commands, such as Insert,delete, Wait a minute. To perform these query requests that do not have a return value, you need to use the <sql:update jstl function. He can execute them directly like <sql:query, just to execute it is not necessary to specify a result variable to store the result of the statement execution return.

Line 18--20 Now that we have executed the select query, we should show him the result of his return. <c:foreach is a function that has an iterative function in Jstl, when we execute it, we return each data line information returned to the variable ${row by ${result.rowsbyindex}, and then on line 19th we pass <c:out value= "$ {row[0]} "/> 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:

<?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 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 Jstl or Tomcat manuals, and you should read these manuals if you want to understand how they work and why they work. However, in order to help you quickly grasp the basic operation of JSTL, I have introduced the relevant knowledge.

If you are using other JSP containers, then you need to read their relevant manual material.

Rorososo so much, I would also like to say that this article is just a basic introduction to JSTL technology, not a complete manual. Jstl contains many functional usages to help you complete your JSP development in a simple and quick way, I suggest you read some more detailed documentation on the JSTL feature and how it works with JavaBeans, and you may find out that he is the development platform you've been waiting for. By reading this article, you should already be able to create some simple front and back desk applications based on SQL databases.



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.