Database Reference
In-Depth Information
5. The
mcb/WEB-INF
directory contains a file named
jstl-mcb-setup.inc
. This file is
not part of JSTL itself, but it contains a JSTL
<sql:setDataSource>
tag used by
many of the
mcb
JSP pages to set up a data source for connecting to the
cookbook
database. The file looks like this:
<sql:setDataSource
var=
"conn"
driver=
"com.mysql.jdbc.Driver"
url=
"jdbc:mysql://localhost/cookbook"
user=
"cbuser"
password=
"cbpass"
/>
Edit the
url
,
user
, and
password
tag attributes if necessary to change the connection
parameters to those that you use for accessing the
cookbook
database. Do not change
the
var
attribute, which names the variable to associate with the connection. By
convention,
mcb
JSP pages in
MySQL Cookbook
use the variable
conn
; tags occurring
later in the page that require a data source refer to the connection using the ex‐
pression
${conn}
.
6. The JSTL distribution also includes WAR files containing documentation and ex‐
amples (
standard-doc.war
and
standard-examples.war
). If you want to install these,
copy them into Tomcat's
webapps
directory. (I recommand that you install the doc‐
umentation so that you can access it locally from your own server. It's useful to
install the examples as well because they provide helpful demonstrations showing
how to use JSTL tags in JSP pages.)
7. Restart Tomcat so it notices the changes you just made to the
mcb
application and
unpacks the WAR files containing the JSTL documentation and examples.
After installing JSTL and restarting Tomcat, request the following
mcb
application page
to verify that Tomcat can find and use the JSTL tag library properly:
http://localhost:8080/mcb/jdbc_jstl_test.jsp
Use these URLs to access the documentation and examples:
http://localhost:8080/standard-doc/
http://localhost:8080/standard-examples/
Writing a MySQL script using JSP and JSTL
Recipe 18.2
shows how to write Perl, Ruby, PHP, and Python versions of a script to
display the names of the tables in the
cookbook
database. With the JSTL tags, we can
write a corresponding JSP page that provides that information:
<%-
-
show_tables
.
jsp
:
Display
names
of
tables
in
cookbook
database
--
%>
<%@
taglib
prefix
=
"c"
uri
=
"http://java.sun.com/jsp/jstl/core"
%>
<%@
taglib
prefix
=
"sql"
uri
=
"http://java.sun.com/jsp/jstl/sql"
%>
<%@
include
file
=
"/WEB-INF/jstl-mcb-setup.inc"
%>