Accelerate JSP access to databases in JDBC Design

Source: Internet
Author: User
Tags informix tld

Previously, we have mentioned that JSPProgramIt is a module and has a powerful expression-Request function. Establishing a perfect database access is a challenging process, and the JDBC interface can well complete this process. However, embedded in JSPCodeThe JDBC code in is the same as that of SQL commands embedded in JDBC. jsp functions can be fully utilized 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 articleArticleWe discussed how to embed JDBC in 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.