O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

23.7. Using A SQL JSTL Tag with a DataSource Configuration

Problem

You want to interact with a relational database by configuring the javax.sql.DataSource in the deployment descriptor.

Solution

Add a context-param element to web.xml, creating a parameter named javax.servlet.jsp.jstl.sql.dataSource that connects with a particular database.

Discussion

The JSTL SQL tag library allows a JSP to interact with a database using custom tags. Java Database Connectivity (JDBC) and the classes in the javax.sql package allow this technology to work. The first step in this recipe is to configure the DataSource that the tags will use to connect with a database.

Tip

A DataSource is a factory for java.sql.Connection objects, which represent a socket connection with a particular database server such as MySQL or Oracle.

Example 23-7 creates a context-param element in web.xml. For the JSTL's SQL tags to automatically receive their Connections from this setting, the param name must be javax.servlet.jsp.jstl.sql.dataSource. The param value comprises comma-separated phrases:

[JDBC URL],[Driver name],[user],[password]

Developers commonly derive the JDBC URL and driver name from database vendor documentation (and often from mailing lists, because debugging backend database connections with JDBC can be tricky!). The code I show here contains an example of a JDBC URL for Oracle8i Personal Edition.

Example 23-7. An example web.xml configuration for a javax.sql.Datasource

<!-- top of web.xml file --><context-param> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required