Java Reference
In-Depth Information
confi gure the JNDI data source. Here are the steps, with details provided in the
sub-sections that follow…
1 . D e fi ne a JNDI resource reference in the Web deployment descriptor, which is a
fi le called web.xml . Every Web application requires the existence of such a fi le,
which must be in <CATALINA_HOME>\webapps\<WebAppName>\WEB-INF .
2. Map the JNDI resource reference onto a real resource (a database) in the context
of the application. This is done by creating a folder called META-INF , alongside
WEB-INF , and placing a fi le called context.xml inside this folder. This fi le will
contain the necessary tags for the mapping. [Normally, META-INF would be
used only within a WAR (Web Archive) fi le, but it will work fi ne without such
and will allow us to keep things as simple as possible.]
3. In the application code, look up the JNDI data source reference to obtain a pooled
database connection. This connection may then be used in the same way that
such connections were used after having been set up via the DriverManager
class in the earlier sections of this chapter.
Details of the three steps listed above are given in the next few sub-sections. For
purposes of illustration, we shall assume the existence of a MySQL database called
Finances that holds a single table called Accounts . The structure of this simple table
will be the same as that specifi ed in Sect. 7.4 for the MS Access table that was used
for illustration in earlier sections of this chapter, the only slight variation being in
the names of the MySQL types. The structure of this is shown below.
Field name
MySQL type
Java type
fi rstNames
fl oat
Defi ning a JNDI Resource Reference
This is achieved by creating a <resource-ref> tag in web.xml . This tag must appear
after the <servlet-mapping> tag(s) and has three named elements:
<res-ref-name> , which specifi es a name for the connection, this name com-
mencing with jdbc/ ;
<res-type> , which identifi es the reference as being of type javax.sql.DataSource
(i.e., a JDBC data source);
<res-auth> , which specifi es that resource authentication will be applied by the
Web Container (Tomcat).
Search WWH ::

Custom Search