Using JSTL to realize rapid development of JSP application

Source: Internet
Author: User
Tags foreach php and table name mysql database tomcat

In this article, I'll introduce a flexible and interesting technique based on JSP, that's JSTL. Jstl is all called the 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 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 need to really install it, just take one of those. The jar file is included in the Web-inf/lib directory of your application, 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; 2nd, that's MySQL is free to download and contains a JDBC driver for Java. In short, in order 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: <!-- 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 just give you some tag information and you don't need to enter them into your code file.) )

The above is all hello.jsp source code, very surprised, he is just pure HTML code, so simple, I think there should be no annotation necessary. The reason I included these snippets in the article is to show you how easy it is to integrate JSTL into HTML sites that need to quickly augment additional functionality. Let me show you all the code for continue.jsp, and after that, you'll have some idea of jstl.


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:
:
: continue< /title>
of

: Url= "Jdbc:mysql://${l}/${d}?user=${u}&password=${p}"/>
: 16:select * from
:
:
:

:
:
:

(请注意文本左侧的数字只是为你提供一些标记信息,你不需要将他们输入到 你的代码文件中。)

这就是我们所有的代码了,是不是非常不错?现在我门来解释一下上面这些代 码的作用。

行1 是 HTML 的注释说明。

行2--3 这些 JSP 标签是为了引用外部的 标签库,更确切的说这里是引用了 JSTL库中的 Core 和SQL标签库部分。我们为他们都设置了前缀名称,以便于我们 通过这些前缀名称访问被引入的标签库中的功能方法。

行4---8正如 Hello.jsp 真正运行的样子,他将请求continue.jsp, Continue.jsp获得请求后就需要得到并解析来自Hello.jsp的若干变量,我们使用 了这样的方式${param.YOUR_VAR}。在第4行<c:set标签,将变量${h}设置为 "localhost",第五行变量${l}将获取我们在 Hello.jsp中的login文本域中输入的 信息,第6,7,8行的变量将分别获得来自Hello.jsp中用户所输入的密码,数据 库名称,数据表名称。

行9--13是一些我经常用来常见HTML网页头的简单的HTML标签,马上,重要的 功能马上要到了。

行14,我们试图用我们先前获得的mysql驱动程序(com.mysql.jdbc.Driver)建 立数据库连接,在URL中,我们指明数据库连接所需要的参数,象数据库名称,主 机名称,登陆名及登陆密码。依此,我们可以用其它任何的JDBC驱动程序连接其 相对的数据库。如果我们需要连接其他的SQL数据库,只需更改这个 URL就够了。

行15--17 这里我们执行了一个Select 查询,请特别注意第16行,我们使用了 另一个JSTL功能函数<c:out 用来输出我们所获取的数据表名称,这里我们同样 可以使用其他的SQL命令,比如说 INSERT,DELETE,等等。要执行这些没有返回值 的查询请求,你就需要使用<sql:update JSTL功能函数了。他可以直接执行他们 就象<SQL:query一样,只是执行它是不需要特别指定结果变量来存储语句执行返 回的结果。

行18--20 既然我们执行了上面的SELECT查询语句,我们就应该把他的返回结 果显示出来。<c:forEach 就是JSTL 中一个具有迭代功能的函数,执行它时,我 们通过${result.rowsByIndex}将返回的每一个数据行信息返回给变量${row},紧 接着在第19行, 我们通过<c:out value="${row[0]}"/>显示返回的每个数据行 的第一个数据列中的值。只要你的数据表中包含的字段,你都可以通过改变变量 ${row}中的数字大小来对请求表中的任意一个字段中的值对他们进行访问。

行21--22是HTML 页脚

在你亲自创建JSTL应用程序过程中,可能你还没有发现它有多么的强大,但你 应该能够意识到JSTL的功能函数的简单和高效性,试想有了JSTL,整合出一个基于 SQL的新闻专栏是何等的快速,集成你现有的web站点是何等的容易。

很好,我们的代码很容易理解的,即使一个非专业程序员的话,打个比方就算 是一个设计师也能够读懂它,理解它,还可能进行部分的修改,起码是页面布局 上的修改。

正如我们开始的时候所提到的,为了使我们的JSTL代码正常的运行,我们需要 安装Mysql Connector/J 中的JAR 文件,当然还有 JSTL。因为我们使用了Tomcat 这个JSp 容器,你需要在Tomcat的文件目录Webapps下创建你自己的文件夹,将你 的Hello.jsp 、Continue.jsp文件放在你创建的文件目录中,在你创建的目录中 你还要创建一个叫做WEB-INF 的文件夹,将你的配置文件Web.xml放在里面, web.xml文件如下:

<?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 />

接下来我们还需要在WEB-INF下创建一个叫做lib的子目,并将下列文件放进去 :

jstl.jar

saxpath.jar

standard.jar

mysql-connector-java-3.0.9-stable-bin.jar (注意,这个名称依据你的 Mysql Connector/J版本不同可能会有所变化)

所有的这些信息您能够在JSTL或Tomcat手册中查阅的到,如果你想理解他们具 体是如何运行而又为什么这么运行你就应该研读一下这些手册。不过为了帮助你 快速的掌握JSTL的基本操作,相关的知识我已经作了介绍。

如果你使用的是其他的Jsp 容器,那么就需要你阅读他们相关的手册资料。

罗罗索索这么多,我还想再说明一点,这篇文章只是对JSTL技术的基本的介绍 ,并不是完全手册。JSTL中包含有许多功能丰富的函数用法来帮助你以简捷快速 的方式完成你的Jsp开发,我建议大家读一些有关JSTL功能以及它如何与 JavaBeans协同工作的更详细的文档资料,到最终你可能会发现他就是你期待已久 的开发平台。通过阅读本篇文章,你应该已经能够创建一些简单的基于SQL 数据 库的前后台应用了。

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.