Can JSTL use connection pool? Can anyone give an example scripts and configuration files?

Laurent Mihalkovic

JSTL allows you to use the traditional Driver based approach or a DataSource implementation. The configuration is done either via the Web.xml file or using the JSTL API.

<sql:setDataSource> has the following attributes:

Attribute  Description                                    Required Default
driver     Name of the JDBC driver class to be registered No       None
url        JDBC URL for the database connection           No       None
user       Database username                              No       None
password   Database password                              No       None
dataSource Database prepared in advance (String or
No None
var        Name of the variable to represent the database No       Set default
scope      Scope of the variable to represent the database No       page
Here are some samples
1. Use a database URL

The connection tag accepts a database URL that can obtain a Connection through Driver Manager:

<%-- open a database connection --%>
<sql:connection id="conn1">

  <%-- required --%>
  <%-- optional --%>
  <%-- optional --%> 
  <%-- optional --%>

The "id" attribute is required by every "connection" tag. After the end tag, a java.sql.Connection object will be added as a pageContext attribute, and it can then be referenced by other tags, including statement, preparedStatement, and closeConnection.

Instead of including your database URL, driver name, user id, or password inside your tag body, you may optionally use the "initParameter" attribute:

<%-- store your connection info in the web.xml file --%>
<sql:connection id="conn1">
  <sql:url initParameter="dbURL"/> 
  <sql:driver initParameter="mysqlDriver"/>
  <sql:userId initParameter="dbUserId"/> 
  <sql:password initParameter="dbPassword"/>
2. Use a DataSource object

The connection tag also accepts a reference to a Servlet attribute containing a javax.sql.DataSource object. (The attribute is found via the findAttribute() method of PageContext.):

<%-- open a database connection --%>
<sql:connection id="conn1" dataSource="ds1">
  <%-- optional --%> 
  <%-- optional --%>

3. Use a JNDI named JDBC DataSource

The Connection tag also accepts a JNDI named JDBC DataSource.

<%-- open a database connection --%>
<sql:connection id="conn1" jndiName="java:/comp/jdbc/test"/>
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.