Java Reference
In-Depth Information
Sidebar 16.3 Database resources in JSP
The use of a database from a Java web application is more complex than the use
of a JDBC connection from a normal Java program. It requires setting up a
resource that will be used by the web application.
The exact configuration depends on the container and database. Here we
assume the use of the Tomcat servlet and JSP container, an open source that is
freely available from the Jakarta Project, http://jakarta.apache.org/. The database
we use is the Cloudscape DB that is bundled with the Java 2 SKD Enterprise
Edition.
First, we need to make the database jar files available to the web container. In
our case we have to copy the
cloudscape.jar
and the
RmiJdbc.jar
files to the
common
lib directory of Tomcat.
Then we need to declare a
DataSource
in the configuration of the JSP container.
In our case we need to add the following fragment to the
server.xml
file:
<Resource name
#
"jdbc/CloudscapeDB" auth
#
"Container"
type
#
"javax.sql.DataSource" scope
#
"Shareable"/>
<ResourceParams name
#
"jdbc/CloudscapeDB">
<parameter><name>user</name><value></value></parameter>
<parameter><name>password</name><value></value></parameter>
<parameter><name>driverClassName</name>
<value>COM.cloudscape.core.RmiJdbcDriver</value></parameter>
<parameter><name>driverName</name>
<value>jdbc:rmi:jdbc:cloudscape:CloudscapeDB</value>
</parameter></ResourceParams>
Finally the descriptor of the application must include a reference to the
resource declared in the container configuration. The
web.xml
file of the
application contains an entry like this:
<resource-ref>
<res-ref-name>
jdbc/CloudscapeDB
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>
The
Cloudscape
DB is based on the
RmiJdbc
connector, thus the database runs as
a separate process. Before starting the server we need to start the Cloudscape DB
with the
cloudscape -start
command from the
bin
directory of the J2SDKEE.
The JSP
login
authenticates the user and, in the case of success, redirects
the user's browser to the main page; the authentication is performed by the
object manager of class
Manager
that is registered in the application context.
If the authentication is successful the user object is registered in the session
context.