Previously, we have mentioned that JSP programs are modules and have powerful presentation-request functions. Establishing a perfect database access is a challenging process, and the JDBC interface can complete this process well. However, JDBC code embedded in JSP code, just like SQL commands embedded in JDBC, can make full use of the capabilities of JSP to create a clean and simple API for clients. In order to achieve this purpose, we can consider using JSP operations to establish database interface components.
The perfect JSP design pattern is Model-View-Controller (MVC). The traditional three-tier system is: Model is for program logic and data; View is for viewing; and Controller is for request processing. Following this model, a JSP program contains pages for each "row" of a client-server "dialog box". In a typical program, you might see a query page, a validation page, a database insert page, a database update page, and so on.
In the previous article, we discussed how to embed JDBC in each page to ensure that the structure of the program is more reasonable. However, creating executable SQL commands, as well as variables passed through JDBC commands, may also increase the complexity of the program.
JSP operations designed by JDBC
Another approach to JSP database operations is to create a collection of operations for the database without using JDBC. Using this approach, you can get two benefits: first, you can eliminate the need to use JDBC, which simplifies a lot of work; second, your design and code organization are more reasonable (such as readability, flexibility performance, and maintainability).
You still need some drivers, but you simplify the above first. The operations in a JSP program are logical blocks that are usually written and used by other JSP program developers, but you can use them as subroutines. The significance of using JSP operations is to standardize certain functions and minimize the amount of Java code embedded in JSP.
JSP provides a set of standard extension classes. Through these classes, you can define an operation through a tag handler. There are two Java interfaces defined by JSP: Tag interface and BodyTag interface, which are executed by TagSupport class and BodyTagSupport class respectively.
You can create a tag library for general JSP purposes, and you can also implement tag handlers to extend class support. Here are the steps to implement these processes.
First, execute a tag manager class:
packagecom.myactions;
(import statements go here)
public class MyActionTag extends TagSupport {
...
}
Next, compile this code and place the class file in the program's class library. Then, you will need a Tag Library Descriptor (TLD) file, which is an XML file that matches your action name and the corresponding tag manager's class.
<tag>
<name>MyAction</name>
<tagclass>com.myactions.MyActionTag</tagclass>
<bodycontent> (whatever) </bodycontent>
<attribute>myData</attribute>
</tag>
</tag>
Related Trialware
Assume that you have created an action named MyAction, which is a TLD that matches the com.myactions.MyActionTag class. The TLD file must be located in the program's TLDs path.
When you call an operation from a JSP page, the TLD tells the JSP the correct class to use the operation. This brings great convenience and requires only a small amount of code.
But where to introduce SQL? First, you need to establish database access with connection capabilities. You can do this using the javax interface, which is available in the JDBC 2.0 Optional toolbox. JDBC 2.0's javax.sql.DataSource class provides the connection you need.
Where is SQL located at this point? It's in the bean. You can use JDBCcreateStatement and PreparedStatement to create a method in a bean. Make this method a public Vector and pass your SQL statements to this method correctly.
Summary Your database bean executes a SQL statement embedded in the action body. You can pass a statement to the SQL statement, or use it to perform a pre-operation. You can perform your actions through the tag manager. Because JDBC is embedded in the library code, you will not be able to use it explicitly in a JSP program.
At first glance this approach may seem more complex than embedded SQL in JDBC, and embedded JDBC in JSP, but since you build SQL operations and store them in a TLD, you only have to do them once, in all JSP programs. You can access these operations. This is the advantage of this approach.
Next time, we will talk about data transfer between JSP pages and sessions.