Jan. 2, 2008, 4:48 p.m.
posted by reo
Accessing Databases from Web ApplicationsData that is shared between Web components and persistent between invocations of a Web application is usually maintained by a database. Web applications use the JDBC 2.0 API to access relational databases. For information on this API, see: http://java.sun.com/docs/books/tutorial/jdbc 1 The ExamplesThe examples discussed in the chapters Java Servlet Technology (page 367), JavaServer Pages Technology (page 401), Custom Tags in JSP Pages (page 431), and JavaServer Pages Standard Tag Library (page 465) require a database. For this release we have tested the examples with the Pointbase database and we provide an ant build file to create the database tables and populate the database. The remainder of this section describes how to install and start the Pointbase database server, set up the example tables, configure the Web application to use the database, and configure Tomcat to recognize the database. 2 Downloading and Starting the Database ServerYou can download a copy of the Pointbase database from: After you have downloaded and installed the Pointbase database, you will need to do the following:
3 Populating the Database
4 Configuring the Web Application to Use the DatabaseIn order to access a database from a Web application you must declare resource reference in the application's Web application deployment descriptor (see References to Environment Entries, Resource Environment Entries, or Resources (page 358)). The resource reference declares the name and type of resource and the type of authentication used when the resource is accessed. 5 Configuring the Server to Recognize the DatabaseSince the resource reference declared in the Web application deployment descriptor uses a JNDI name to refer to the database, you must connect the name to an actual database by providing a resource factory in the Tomcat's configuration. Here is the resource factory used by the application discussed in all the Web technology chapters:
<Resource name="jdbc/BookDB" reloadable="true"
auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/BookDB">
<parameter>
<name>user</name>
<value>public</value>
</parameter>
<parameter>
<name>password</name>
<value>public</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.pointbase.jdbc.jdbcUniversalDriver</value>
</parameter>
<parameter>
<name>driverName</name>
<value>jdbc:pointbase:
server://localhost/sample</value>
</parameter>
</ResourceParams>
Since the resource factory is a subentry of the Context entry described in Running Web Applications (page 360), you add this entry to Tomcat's configuration in the same ways that you can add the Context entry. |
- Comment