JSP programs are all modules and have powerful expression-request functions. Establishing a perfect database access is a challenging process, and the JDBC interface can well complete this process. However, the JDBC code embedded in JSP code, like SQL commands embedded in JDBC, can make full use of the JSP function to create a clean and easy API for the client. To achieve this goal, we can consider using JSP operations to create database interface components.
The perfect JSP design mode is Model-View-Controller (MVC ). The traditional three-tier system is: Model for Program Logic and data; View for viewing; and Controller for request processing. Following this model, a JSP program contains each "line" page of the client-server "dialog box. In a typical program, you can see a query page, a verification page, a database insertion page, a database update page, and so on.
JSP operations designed by JDBC
Another way to operate a JSP database is to create a set of operations for the database without using JDBC. Using this method, 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, and maintainability ).
You still need some drivers, but you need to simplify the above operations first. Operations in JSP programs are logical blocks which are usually written and used by other JSP program developers. However, you can use them as subprograms. Using JSP operations is to standardize some functions and minimize the number of Java code embedded in JSP.
JSP provides a set of standard extended classes. Through these classes, you can define an operation through a tag handler. There are two Java interfaces defined in JSP: Tag interface and BodyTag interface, which are executed by TagSupport class and BodyTagSupport class respectively.
You can create a tag library for common JSP purposes, and you can also execute tag handler to extend the class support. Follow these steps.
First, execute a label manager class:
- packagecom.myactions;
- (import statements go here)
- public class MyActionTag extends TagSupport {
- ...
- }
Next, compile the code and place the class file in the class library of the program. Then, you will need a Tag Library Descriptor (TLD) file, which is an XML file to match your operation name and the corresponding Tag manager class.
- MyAction
- com.myactions.MyActionTag
- (whatever)
- myData
Related 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
Assume that you have created an operation 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, TLD tells the JSP 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 the connection function. You can use the javax interface, while javax is visible in the JDBC 2.0 Optional toolbox. The javax. SQL. DataSource class of JDBC 2.0 provides the connection you need.
Where is the SQL statement located? It is in bean. You can use JDBCcreateStatement and PreparedStatement to create a method in bean. Turn this method into a public Vector and pass your SQL statement to this method correctly.
Summary
Your Database bean executes an SQL statement embedded in the operator body. You can pass a statement to the SQL statement or use it to execute a pre-operation. You can use the tag manager to perform your operations. Since JDBC is embedded in the library code, you cannot explicitly use it in JSP programs.
The first time you use this method, you will think it is more complicated than the embedded SQL in JDBC and the embedded JDBC in JSP. However, you only need to do it once to create SQL operations and store them in a TLD, you can access these operations in all JSP programs. This is the advantage of this method.
- How to Improve Servlet and JSP Application Efficiency
- Pass Chinese parameters between Flex and Jsp
- Brief Introduction to JSP database links
- China's IPTV research goals are world-leading
- 3G and IPTV are the Development Trend