Core JSTL[c] Mastering the JSP Standard Tag Library
9.3 How JSTL Locates Data Sources
Three JSTL SQL actions access a database: <sql:query>, <sql:update>, and <sql:transaction>. Those actions use a data source to access a database, so they all have an optional dataSource attribute that specifies that data source. When you use those actions, JSTL locates a data source with this algorithm: Locate a value:
Let's take a closer look at how the preceding steps work: Locate a value: You can specify a data source with the dataSource attribute for <sql:query>, <sql:update>, or <sql:transaction>; for example, you can execute a database query like this: <sql:query var='customers' dataSource='${myDataSource}' > SELECT * FROM CUSTOMERS </sql:query> In the preceding code fragment, the scoped variable myDataSource can reference either a string or an instance of javax.sql.DataSource . If that attribute references an instance of javax.sql.DataSource , that data source is used by the query; if it references a string, JSTL interprets the string as either a JNDI resource or JDBC parameters. You can also specify a string for the dataSource attribute directly, like this: <sql:query var='customers' dataSource='aJNDIResourceOrJDBCParameters' > SELECT * FROM CUSTOMERS </sql:query> You don't have to specify the dataSource attribute for <sql:query>, <sql:update>, or <sql:transaction>; for example, you can execute a database query like this: <sql:query var='customers'> SELECT * FROM CUSTOMERS </sql:query> If you omit the dataSource attribute, JSTL assumes that the value of the SQL_DATA_SOURCE configuration setting, if it exists, represents a data source. If the SQL_DATA_SOURCE , configuration setting does not exist and you don't specify the dataSource attribute, the <sql:query>, <sql:update>, and <sql:transaction> actions will throw an exception. You can specify a value for the SQL_DATA_SOURCE configuration setting in the deployment descriptor, with a business component, or with the <sql:setDataSource> action. See "Creating Data Sources" on page 365 for more information about setting the SQL_DATA_SOURCE configuration setting. Evaluate the value: The value that you specify for a data source can be a string or an instance of javax.sql.DataSource . If it's the latter, JSTL uses it as is; if it's the former, continue to the next step. Access the Data Source: If you specify a data source as a string, JSTL first assumes that the string is a relative path to a JNDI resource and tries to retrieve that resource from the JSP container's JNDI naming context. If the JNDI lookup fails, then <sql:query>, <sql:update>, and <sql:transaction> assume that the string represents JDBC parameters and try to create a JDBC connection. If you specify a data source as a string and that string is not a valid JNDI relative path and does not represent valid JDBC parameters, then <sql:query>, <sql:update>, and <sql:transaction> will throw an exception. Summary: You can specify a data source as:
You can specify a data source with:
This section discussed how JSTL locates a data source; how you create a data source and make it available to JSTL is the topic of the next section. |