Accelerate JSP access to databases in JDBC Design

Source: Internet
Author: User
Tags informix

Previously, we have mentioned that 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.

In the previous article, we discussed how to embed JDBC into each page to ensure a more rational program structure. However, creating executable SQL commands, just as variables are passed through JDBC commands, may increase the complexity of the program.

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.

<Tag>
<Name> MyAction </name>
<Tagclass> com. myactions. MyActionTag </tagclass>
<Bodycontent> (whatever) </bodycontent>
<Attribute> myData </attribute>
</Tag>

</Tag>

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)
     MoreTrialware
  • 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.

    Next time, we will talk about data transmission between JSP pages and sessions.

    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.