•
The JSTL dist ribut ion also includes WAR files cont aining docum ent at ion and exam ples st andard-doc.war and st andard-exam ples.war . I f you wish t o inst all t hese, copy
t hem int o Tom cat s webapps direct ory. You probably should inst all t he docum ent at ion so t hat you can access it locally from your own server. I t s useful t o inst all t he
exam ples as well, because t hey provide helpful dem onst rat ions showing how t o use JSTL t ags in JSP pages.
•
Rest art Tom cat so t hat it not ices t he changes youve j ust m ade t o t he
mcb
applicat ion and so t hat it unpacks t he WAR files cont aining t he JSTL docum ent at ion and exam ples.
I f Tom cat doesnt unpack WAR files for you aut om at ically, see t he sidebar Unpacking a
WAR File Manually .
Aft er inst alling JSTL, rest art Tom cat and request t he following
mcb
applicat ion page t o verify t hat Tom cat can find t he JSTL t ags properly:
ht t p: t om cat .snake.net : 8080 m cb j st l_t est .j sp
16.4.7 Writing JSP Pages with JSTL
This sect ion discusses t he synt ax for som e of t he JSTL t ags used m ost frequent ly by
mcb
JSP pages. The descript ions are very brief, and m any of t hese t ags have addit ional at t ribut es t hat
allow t hem t o be used in ways ot her t han t hose shown here. For m ore inform at ion, consult t he JSTL specificat ion see
Appendix C .
A JSP page t hat uses JSTL m ust include a
taglib
direct ive for each t ag set t hat t he page uses. Exam ples in t his book use t he core and dat abase t ags, ident ified by t he following
taglib
dir ect ives: taglib uri=http:java.sun.comjstlsql prefix=sql
taglib uri=http:java.sun.comjstlcore prefix=c The
uri
values should m at ch t he sym bolic values t hat are list ed in t he web.xm l
taglib
ent ries see Recipe 16.4.6
. The
prefix
values indicat e t he init ial st ring used in t ag nam es t o ident ify t ags as part of a given t ag library.
JSTL t ags are writ t en in XML form at , using a special synt ax for t ag at t ribut es t o include expressions. Wit hin t ag at t ribut es, t ext is int erpret ed lit erally unless enclosed wit hin
{...}
, in which case it is int erpret ed as an expression t o be evaluat ed. The following t ags are part of t he JSTL core t ag set :
c:out
This t ag evaluat es it s
value
at t ribut e and is replaced by t he result . One com m on use for t his t ag is t o provide cont ent for t he out put page. The following t ag would
produce t he value 3: c:out value={1+2}
c:set
This t ag assigns a value t o a variable. For exam ple, t o assign a st ring t o a variable nam ed
title
and t hen use t he variable lat er in t he
title
elem ent of t he out put page, do t his:
c:set var=title value=JSTL Example Page html
head titlec:out value={title} title
head ...
This exam ple illust rat es a principle t hat is generally t rue for JSTL t ags: To specify a variable int o which a value is t o be st ored, nam e it wit hout using
{...}
not at ion. To refer t o t hat variables value lat er, use it wit hin
{...}
so t hat it is int erpret ed as an expression t o be evaluat ed.
c:if
This t ag evaluat es t he condit ional t est given in it s
test
at t ribut e. I f t he t est result is t rue, t he t ag body is evaluat ed and becom es t he t ags out put ; if t he result is false, t he
body is ignored: c:if test={1 = 0}
1 is not equal to 0 c:if
The com parison operat ors are
==
,
=
, ,
,
=
, and
=
. The alt ernat ive operat ors
eq
,
ne
,
lt
,
gt
,
le
, and
ge
m ake it easier t o avoid using special HTML charact ers in expressions. Arit hm et ic operat ors are
+
,
-
, ,
or
div
, and or
mod
. Logical operat ors are
and
,
|| or
, and
not
. The special
empty
operat or is t rue if a value is em pt y or
null
: c:set var=x value=
c:if test={empty x} x is empty
c:if c:set var=y value=hello
c:if test={empty y}
y is not empty c:if
c:choose
This is anot her condit ional t ag, but it allows m ult iple condit ions t o be t est ed. I nclude a
c:when
t ag for each condit ion t hat you want t o t est explicit ly, and a
c:otherwise
t ag if t here is a default case: c:choose
c:when test={count == 0} Please choose an item
c:when c:when test={count 1}
Please choose only one item c:when
c:otherwise Thank you for choosing an item
c:otherwise c:choose
c:forEach
This t ag act s as an it erat or, allowing you t o loop over a set of values. The following exam ple uses a
c:forEach
t ag t o loop t hrough a set of rows in t he result set from a query represent ed here by t he
rs
variable : c:forEach var=row items={rs.rows}
id = c:out value={row.id} , name = c:out value={row.name}
br c:forEach
Each it erat ion of t he loop assigns t he current row t o t he variable
row
. Assum ing t hat t he query result includes colum ns nam ed
id
and
name
, t he colum n values are accessible as
row.id
and
row.name
. The JSTL dat abase t ags are used t o issue queries and access t heir result s:
sql:setDataSource
This t ag set s up connect ion param et ers t o be used when JSTL cont act s t he dat abase server. For exam ple, t o specify param et ers for using t he MySQL Connect or J driver t o
access t he
cookbook
dat abase, t he t ag looks like t his: sql:setDataSource var=conn
driver=com.mysql.jdbc.Driver url=jdbc:mysql:localhostcookbook
user=cbuser password=cbpass
The
driver
,
url
,
user
, and
password
at t ribut es specify t he connect ion param et ers, and t he
var
at t ribut e nam es t he variable t o associat e wit h t he connect ion. By convent ion, JSP pages in t his book use t he variable
conn
, so t ags occurring lat er in t he page t hat require a dat a source can refer t o t he connect ion using
t he expression
{conn}
. To avoid list ing connect ion param et ers in each JSP page t hat uses MySQL, a
sql:setDataSource
t ag for connect ing t o t he
cookbook
dat abase is placed in t he include file WEB- I NF j st l- m cb- set up.inc. A JSP page can access t he file as
follows t o set up t he connect ion: include file=WEB-INFjstl-mcb-setup.inc
To change t he connect ion param et ers used by t he
mcb
pages, j ust edit j st l- m cb- set up.inc.
sql:update
To issue a st at em ent such as
UPDATE
,
DELETE
, or
INSERT
t hat doesnt ret urn rows, use a
sql:update
t ag. A
dataSource
t ag at t r ibut e indicat es t he dat a source, t he affect ed- rows count result ing from t he st at em ent is ret urned in t he
variable nam ed by t he
var
at t ribut e, and t he st at em ent it self should be specified in t he t ag body:
sql:update var=count dataSource={conn} DELETE FROM profile WHERE id 100
sql:update Number of rows deleted: c:out value={count}
sql:query
To process queries t hat ret urn a result set , use
sql:query
. As w it h
sql:update
, t he t ext of t he query is given in t he t ag body, and t he
dataSource
at t ribut e indicat es t he dat a source. The
sql:query
t ag also t akes a
var
at t ribut e t hat nam es t he variable you want t o associat e wit h t he result set :
sql:query var=rs dataSource={conn} SELECT id, name FROM profile ORDER BY id
sql:query
The
mcb
JSP pages use
rs
as t he nam e of t he result set variable. St rat egies for accessing t he cont ent s of a result set are out lined below.
sql:param
You can writ e dat a values lit erally int o a query st ring, but JSTL also allows t he use of placeholders, which is helpful for values t hat cont ain charact ers t hat are special in SQL
st at em ent s. Use a
?
charact er for each placeholder in t he query st ring, and provide values t o be bound t o t he placeholders using
sql:param
t ags in t he body of t he query- issuing t ag. The dat a value can be specified eit her in t he body of t he
sql:param
t ag or in it s
value
at t r ibut e: sql:update var=count dataSource={conn}
DELETE FROM profile WHERE id ? sql:param100sql:param
sql:update sql:query var=rs dataSource={conn}
SELECT id, name FROM profile WHERE cats = ? AND color = ? sql:param value=1
sql:param value=green sql:query
The cont ent s of a result set ret urned by
sql:query
are accessible several ways. Assum ing t hat a result set is available t hrough a variable
rs
, r ow
i
of t he result can be accessed eit her as
rs.rows[ i
]
or as
rs.rowsByIndex[ i
]
, where row num ber values begin at 0. The first form produces a row wit h colum ns t hat can be accessed by nam e.
The second form produces a row wit h colum ns t hat can be accessed by colum n num ber beginning wit h 0 . For exam ple, if a result set has colum ns nam ed
id
and
name
, you can access t he values for row t hree using colum n nam es like t his:
c:out value={rs.rows[2].id} c:out value={rs.rows[2].name}
To use colum n num bers inst ead, do t his: c:out value={rs.rowsByIndex[2][0]}
c:out value={rs.rowsByIndex[2][1]} You can also use
c:forEach
as an it erat or t o loop t hrough t he row s in a result set . I t erat e t hrough
rs.rows
if you want t o access colum n values by nam e: c:forEach var=row items={rs.rows}
id = c:out value={row.id} , name = c:out value={row.name}
br c:forEach
I t erat e t hrough
rs.rowsByIndex
t o access colum n values by num ber:
c:forEach var=row items={rs.rowsByIndex} id = c:out value={row[0]} ,
name = c:out value={row[1]} br
c:forEach
The row count is available as
rs.rowCount
: Number of rows selected: c:out value={rs.rowCount}
Nam es of t he colum ns in t he result set are available using
rs.columnNames
: c:forEach var=name items={rs.columnNames}
c:out value={name} br
c:forEach
16.4.8 Writing a MySQL Script using JSP and JSTL