Table of Contents

preface xix
acknowledgments xxi
about this book xxiii
About the cover illustration xxvii

Part 1  Background   1


1  Dynamic web sites   3
1.1 The boring life of a web browser 4
1.2 The simple ideas behind dynamic web content 6
1.3 What you need to run JSTL 9
JSP containers 10
1.4 Real-world web applications 11
1.5 Summary 14

2  Foundation: XML and JSP   15
2.1 Introduction to XML 16
A dose of tag terminology 17
The relevant rules of XML 21
2.2 Introduction to JSP 23
JSP tag syntax 24
Standard JSP tags 25
JSP tag libraries 29
Other JSP directives 32
JSP comments 33
How JSP organizes data 34
2.3 Summary 39


Part 2  Learning JSTL   41


3  The expression language   43
3.1 Expressions and the <c:out> tag 44
What expressions look like 45
Where expressions work 46
Default values in <c:out> 46
Special characters and <c:out> 47
3.2 Scoped variables and the expression language 48
Basic syntax to access scoped variables 48
Different types of scoped data 50
3.3 Request parameters and the expression language 55
HTML forms 55
A page that reads request parameters 62
3.4 More powerful expressions 63
Different ways to access properties 64
Accessing other data with the expression language 65
Comparisons 67
Boolean operations and parentheses 69
Multiple expressions 70
3.5 Saving data with <c:set> 71
3.6 Deleting data with <c:remove> 74
3.7 Summary 75

4  Controlling flow with conditions   77
4.1 Different kinds of decisions 79
4.2 Yes-or-no conditions with <c:if> 79
The basic syntax of <c:if> 80
Using <c:if> within HTML tags 82
Multiple <c:if> tags 82
Nested <c:if> tags 84
The var and scope attributes 86
4.3 Mutually exclusive conditions with <c:choose>, <c:when>, and <c:otherwise> 88
Why JSTL has complex conditional tags 88
How the complex con-ditional tags work 89
Rules for using the complex conditional tags 93
4.4 Summary 94

5  Controlling flow with loops   95
5.1 General-purpose looping with <c:forEach> 96
5.2 Iterating over strings with <c:forTokens> 98
How JSTL parses strings 100
5.3 Advanced iteration with <c:forEach> and <c:forTokens> 101
Looping over part of a collection 101
Looping over numbers 104
Loop status 106
5.4 Loop example: scrolling through results 107
Understanding the example 109
Using varStatus in the example 111
5.5 Summary 113

6  Importing text   115
6.1 Including text with the <c:import> tag 117
Absolute and relative URLs 117
Retrieving data from URLs 121
Saving information for later 122
Communicating with imported pages 124
Import example: a customized header 127
6.2 Redirecting with <c:redirect> 132
6.3 Formatting URLs with <c:url> 134
How to use <c:url> 134
Why to use <c:url> 135
6.4 Summary 137

7  Selecting XML fragments   139
7.1 XPath?s vision of an XML document 140
7.2 XPath?s basic syntax 142
Deep descendants 143
Attributes 144
Predicates and element order 145
Strings and booleans 146
7.3 XPath variables and JSTL 147
7.4 JSTL, XPath, and namespaces 148
7.5 More advanced XPath 149
Nodes and axes 149
Contexts 150
Further reading 151
7.6 Summary 152

8  Working with XML fragments   153
8.1 Parsing documents with <x:parse> 154
Sources of XML 155
8.2 Accessing XML with <x:out> and <x:set> 156
Finding a document 156
The <x:out> tag 157
The <x:set> tag 158
8.3 Control flow based on XML documents 159
Simple conditions with <x:if> 160
Compound conditions with <x:choose> 162
Looping over parts of a document with <x:forEach> 164
8.4 XML transformations using JSTL 169
Simple transformations with <x:transform> 170
Using the var attribute 172
XSLT parameters 174
Advantages of using XSLT within JSTL 174
8.5 An XML example: reading RSS files 175
8.6 Summary 179

9  Database-driven pages   181
9.1 When to use JSTL?s database support 183
When to use databases 183
Direct access from JSP pages 183
9.2 Setting up a database connection with <sql:setDataSource> 185
Caution against <sql:setDataSource> 188
9.3 Performing queries with <sql:query> 189
Performing a database query 190
Reading a query?s results 191
Limiting the size of a query?s result 197
9.4 Modifying data with <sql:update> 199
Simple uses of the <sql:update> tag 199
Measuring the effect of an <sql:update> tag 200
9.5 Using <sql:param> with adjustable queries 201
Template queries 202
Safe, convenient parameters with <sql:param> 202
Date parameters with <sql:dateParam> 205
9.6 Managing transactions with <sql:transaction> 206
The <sql:transaction> tag 207
Transaction isolation 210
9.7 SQL example: a hit counter 211
9.8 Summary 213

10  Formatting and internationalization   215
10.1 Printing numbers with <fmt:formatNumber> 216
Basic usage of <fmt:formatNumber> 217
Different ways to specify a value 218
Storing a number instead of printing it 219
Printing different types of numbers: percentages and currencies 219
Grouping digits together ? or not 222
Controlling how many digits print 223
More control: custom number patterns 226
10.2 Printing dates with <fmt:formatDate> 227
Differences from <fmt:formatNumber> 227
Printing times, dates, or both 228
Printing longer or shorter dates and times 229
More control: custom date patterns 230
10.3 Reading numbers with <fmt:parseNumber> 232
Why you might want to parse numbers 233
How <fmt:parseNumber> works by default 233
Changing <fmt:parseNumber>?s parsing rules 235
10.4 Reading dates with <fmt:parseDate> 236
How <fmt:parseDate> parses dates by default 236
Changing how <fmt:parseDate> parses dates 237
10.5 Overriding time zones with <fmt:timeZone> and <fmt:setTimeZone> 238
How JSTL figures out time zones by default 239
Setting time zones for individual tags 239
Long-lasting changes with <fmt:setTimeZone> 240
Temporary changes with <fmt:timeZone> 241
10.6 Overriding locales with <fmt:setLocale> 242
How to identify locales 243
The parseLocale attribute for <fmt:parseNumber> and <fmt:parseDate> 244
10.7 Internationalizing text messages with <fmt:message>, <fmt:param>, <fmt:bundle>, and <fmt:setBundle> 245
Using <fmt:message> 245
Loading a bundle family with <fmt:bundle> and <fmt:setBundle> 247
10.8 Summary 248


Part 3  JSTL in action   249


11  Common tasks   251
11.1 Handling checkbox parameters 252
The HTML form 253
A simple checkbox handler 254
Handling some check boxes specially 256
11.2 Accepting dates 257
The HTML form 258
Handling the form and reading the date 259
11.3 Handling errors 261
Ignoring the issue 262
Catching errors with <c:catch> 263
Passing errors to an error page 266
11.4 Validating input 268
Different kinds of form validation 268
Tasks involved when validating a form 269
A sample form validation 271
11.5 Summary 277

12  Dynamic features for web sites   279
12.1 An online survey 280
What our survey looks like 281
Setting up the survey database 281
Adding survey questions to pages 284
How the survey works 285
12.2 A message board 291
What our message board looks like 291
Setting up the message database 293
Linking to appropriate message boards 294
How the message board works 295
12.3 Summary 300

13  Case study in building a web site   301
13.1 Managing the layout 303
A framework for channels 303
Modular channels 305
13.2 Adding dynamic content 309
Including RSS channels 309
Including other dynamic content 311
13.3 Registering users 313
Modifying the header 313
The registration form 314
Saving the registration 318
The user database 319
13.4 Authenticating users 320
Logging in users 320
Some notes about authentication 321
13.5 Personalizing the site 322
Filling in a form automatically 322
Displaying a chosen RSS feed 324
13.6 Summary 325


Part 4  JSTL for programmers   327


14  Control and performance   329
14.1 Scripting elements and the JSTL rtexprvalue libraries 330
Warning against scripting expressions 331
JSTL?s dual libraries 332
Scripting variables and <jsp:useBean> 333
14.2 Modifying properties with <c:set> 334
14.3 Advanced techniques for importing text 335
Representing imported text as a java.io.Reader 335
Character encoding 337
14.4 Advanced XML parsing and manipulation 338
XML data formats 338
Telling <x:parse> where a document came from 340
Efficient parsing with org.xml.sax.XMLFilter 341
Efficient transformations with javax.xml.transform.Result 343
14.5 Deciphering requests with <fmt:requestEncoding> 343
14.6 Exposing data to JSP pages 344
Saving data to a scope 344
Exposing dynamic data structures 346
Writing JavaBeans 347
14.7 Configuring JSTL 349
Providing default information to JSTL tags 349
Managing database access 352
Managing internationalization 354
14.8 Enforcing good page-authoring habits 355
Requiring script-free pages 356
Enumerating legal tag libraries 357
14.9 Summary 359

15  Using JSTL to develop custom tags   361
15.1 Developing and installing tag libraries 362
JSTL?s support for tag-handler developers 363
The tag-library descriptor (TLD) 363
Installing and using a tag library 366
15.2 Developing conditional tags 366
A simple conditional tag 367
A conditional tag with attributes 370
Integrating custom conditional tags with standard tags 376
Using the expression language 377
15.3 Developing iteration tags 378
A simple loop tag 379
More advanced iteration tags 386
15.4 Summary 387

A  JSTL reference   389
A.1 Expression language syntax 390
A.2 Core tag library 392
A.3 XML tag library 397
A.4 Database tag library 401
A.5 Formatting tag library 404

B  JSTL API (for developers)   409
B.1 Configuration variables 410
B.2 Conditions and loops 413
B.3 Interoperating with JSTL?s database tags 415
B.4 Using JSTL?s localization algorithms 417

C  Database tags and SQL   421
C.1 SQL and <sql:update> 422
C.2 SQL and <sql:query> 427
C.3 SQL miscellany 429
C.4 Summary 430

D  References and resources   433
D.1 JSP Standard Tag Library 434
D.2 XML-related references 435
D.3 Databases 435
D.4 Related standards 436
D.5 Miscellaneous references 437

index 439