js| Access | design | data | database
Previously, we have mentioned that JSP programs are modules and have powerful expression-request (presentation-request) capabilities. Creating a perfect database access is a challenging process, and the JDBC interface is a good way to do this. However, the JDBC code embedded in the JSP code, like the SQL command embedded in JDBC, can take full advantage of the functionality of the JSP, creating a neat and simple API for the client. To do this, we can take into account the use of JSP operations to establish database interface components.
The perfect JSP design pattern is Model-view-controller (MVC). The traditional three-tier system is: Model for program logic and data; view for view; and controller for request processing. Following this model, a JSP program contains a page for each "row" of the Client-Server dialog box. In a typical program, you can 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 program's structure is more reasonable. However, creating an executable SQL command, just as a variable is passed through a JDBC command, can also add complexity to the program.
JDBC-Designed JSP operations
Another way to operate a JSP database is to establish a collection of operations for the database without using JDBC. With this approach, you can get two benefits: first, you can eliminate the need to use JDBC, which makes a lot of work easier; second, your design and code are more organized (for example, readability, flexibility, and maintainability).
You still need some drivers, but you first simplify the above operations. The operations in the JSP program are logical blocks that are usually written and used by other JSP developers, but you can use them as subroutines. The meaning of using JSP operations is to standardize some features and minimize the number of Java code embedded in the JSP.
JSP provides a set of standard extended classes. With these classes, you can define an action through a tag manager (tag handler). There are two JSP-defined Java interfaces: The tag interface and the Bodytag interface, which are executed by the TagSupport class and the Bodytagsupport class respectively.
You can build a tag library for general-purpose JSP purposes, and you can also perform tag manager (tag handler) to extend the support of classes. Here are the steps to implement these procedures.
First, the class that executes a tag manager:
Packagecom.myactions;
(Import statements Go)
public class Myactiontag extends TagSupport {
...
}
Next, compile the 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 to match the name of your operation and the corresponding tag manager class.
<tag>
<name>MyAction</name>
<tagclass>com.myactions.MyActionTag</tagclass>
<bodycontent> (whatever) </bodycontent>
<attribute>myData</attribute>
</tag>
</tag>
Related to Trialware
Dbutils (Apache Software Foundation)
IBM Informix JDBC Driver (IBM)
MySQL connector/j 3.0 ( mysql AB)
sql4x Manager J 2.2 (Macosguru)
Informix Downloads (IBM Red Brick Warehouse 32bit) (Solaris) (IBM)
Informix Downloads (IBM Red Brick Warehouse 64bit) (HPUX) (IBM)
more Trialware
Let's say you've established an operation called Myaction, which is a TLD that matches the Com.myactions.MyActionTag class. The TLD file must be located in the TLDs path of the program.
When you invoke 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 do you introduce SQL? First, you need to set up a database access with connection capabilities. You can use the Javax interface to do this, and javax is visible in the JDBC 2.0 optional Toolbox. The Javax.sql.DataSource class for JDBC 2.0 provides the connections you need.
At this point, where is SQL located? It's in the bean. You can use Jdbccreatestatement and PreparedStatement to create a method in the bean. Make this approach a public vector and correctly pass your SQL declaration to this method.
Summarize
Your database bean executes an SQL statement embedded in the operator, you can pass a statement to the SQL statement, or use it to perform a prior operation. You can perform your actions through the tag manager. Since JDBC is embedded in the library code, you will not be able to use it explicitly in the JSP program.
Using this method for the first time will make it more complicated than embedded SQL in JDBC, and embedded JDBC in the JSP, but you can create SQL operations and store them in a TLD, which you only have to do once, and access these operations in all JSP programs. This is the advantage of this method.
Next time, we'll talk about data transfer between JSP pages and session (sessions).