Accessing a Connection Pool from a JSP Page
See Also
There are many ways of using a
JDBC resource (also known as a data source) in an application. The simplest
way is to access your JDBC resource directly from a JSP file using the JSTL 1.1 tag library. You are recommended
to use this method for
testing purposes and in small web applications only.
To set up a JSP file to use the JSTL 1.1 tag library to access a data source:
- Set up a Sun Java System Application Server connection pool, a JBoss Application Server connection pool, or a Tomcat connection pool.
- Right-click the web application project's Libraries node, click Add Library, and choose JSTL 1.1.
- Expand the project node and the Web Pages node
and double-click the default index.jsp node. (Instead of using the
default index.jsp file, you can
create your own JSP file.)
The JSP file opens in the Source Editor.
Now modify the JSP file to do the following:
- Let the server know that the JSP page uses tags from JSTL's CORE
component and JSTL's SQL component. Paste or type the following near the top of
the JSP file, but below the page directives:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
- Query the database.
Make sure to specify the same JNDI name as specified in the JDBC resource that you defined when
setting up the connection pool on the Sun Java System Application Server, JBoss Application Server, or Tomcat Web Server.
To do so, paste or type code similar to the following below the taglib directives in a JSP file:
<sql:query var="queryresults" dataSource="jdbc/poolDB">
SELECT * FROM CUSTOMER_TBL ORDER BY upper(NAME)
</sql:query>
- Do something with the queried data.
For example, to display the data in a table, replace the
content between the default <body> tags by pasting or typing
the following code into the body:
<table border=1>
<tr>
<th>First</th><th>Last</th>
</tr>
<c:forEach var="row" items="${queryresults.rows}">
<tr>
<td><c:out value="${row.NAME}" /></td>
<td><c:out value="${row.CITY}" /></td>
</tr>
</c:forEach>
</table>
- Make the database driver available to your server.
For example, for the Tomcat Web Server, copy the database driver's JAR file
into the Tomcat Web Server's common/lib folder, within
the IDE's installation folder. For the JBoss Application Server, move the driver JAR file to the
domain's lib directory. So, for PointBase, copy pbclient.jar (or pbembedded.jar)
to the Tomcat Web Server's common/lib folder. For the JBoss Application Server,
copy pbclient.jar (or pbembedded.jar) to the server/default/lib folder,
if you are using the default domain.
|
If you have already started the server, make sure that you restart it after you copy the database
driver's JAR file, so that the server can load the JAR file.
|
- Start the database server.
- Run the JSP file or the application
that contains it.
- See Also
- About the Sun Java System Application Server
- About the JBoss Application Server
- About the Tomcat Web Server
Legal Notices