Вы находитесь на странице: 1из 10

JavaServer Pages, 2nd Edition

Hans Bergsten
Publisher: O'Reilly
August 2002
ISBN: 0-596-00317-X, 684 pages

Filled with useful examples and the depth, clarity, and attention to detail that made the first
edition so popular with web developers, JavaServer Pages, 2nd Edition is completely revised
and updated to cover the substantial changes in the 1.2 version of the JSP specifications, and
includes coverage of the new JSTL Tag libraries-an eagerly anticipated standard set of JSP
elements for the tasks needed in most JSP applications, as well as thorough coverage of
Custom Tag Libraries.

Copyright 2002, 2001 O'Reilly & Associates, Inc. All rights reserved.
Printed in the United States of America.
Published by O'Reilly & Associates, Inc., 1005 Gravenstein Highway North, Sebastopol,
CA 95472.
O'Reilly & Associates books may be purchased for educational, business, or sales
promotional use. Online editions are also available for most titles (http://safari.oreilly.com).
For more information contact our corporate/institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered
trademarks of O'Reilly & Associates, Inc. Java and all Java-based trademarks and logos are
trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other
countries. O'Reilly &x Associates, Inc., is independent of Sun Microsystems. Openwave,
the Openwave logo, and UP.SDK are trademarks of Openwave Systems Inc. All rights
reserved.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and O'Reilly
& Associates, Inc. was aware of a trademark claim, the designations have been printed in caps
or initial caps. The association between the image of a grey wolf and the topic of JavaServer
Pages is a trademark of O'Reilly & Associates, Inc.
While every precaution has been taken in the preparation of this book, the publisher and
the author assume no responsibility for errors or omissions, or for damages resulting from the
use of the information contained herein.

Table of Contents

Table of Contents
Preface ................................................................................................................................... 1
What's in the Book ............................................................................................................. 2
Readers of the First Edition................................................................................................ 2
Audience............................................................................................................................. 3
Organization ....................................................................................................................... 4
About the Examples ........................................................................................................... 8
Conventions Used in This Book......................................................................................... 8
How to Contact Us ............................................................................................................. 9
Acknowledgments for First Edition ................................................................................... 9
Acknowledgments for Second Edition............................................................................. 10
Part I: JSP Application Basics .............................................................................................. 12
Chapter 1. Introducing JavaServer Pages ....................................................................... 13
1.1 What Is JavaServer Pages?......................................................................................... 13
1.2 Why Use JSP? ............................................................................................................ 14
1.2.1 Embedding Dynamic Elements in HTML Pages ................................................ 14
1.2.2 Compilation......................................................................................................... 15
1.2.3 Using the Right Person for Each Task ................................................................ 15
1.2.4 Integration with Enterprise Java APIs................................................................. 16
1.2.5 Other Solutions.................................................................................................... 16
1.2.6 The JSP Advantage ............................................................................................. 18
1.3 What You Need to Get Started................................................................................... 18
Chapter 2. HTTP and Servlet Basics................................................................................ 20
2.1 The HTTP Request/Response Model......................................................................... 20
2.1.1 Requests in Detail................................................................................................ 21
2.1.2 Responses in Detail ............................................................................................. 23
2.1.3 Request Parameters ............................................................................................. 25
2.1.4 Request Methods ................................................................................................. 25
2.2 Servlets ....................................................................................................................... 27
2.2.1 Advantages over Other Server-Side Technologies ............................................. 27
2.2.2 Servlet Containers ............................................................................................... 29
2.2.3 Servlet Contexts and Web Applications.............................................................. 30
Chapter 3. JSP Overview................................................................................................... 32
3.1 The Problem with Servlets ......................................................................................... 32
3.2 The Anatomy of a JSP Page....................................................................................... 34
3.3 JSP Processing............................................................................................................ 34
3.3.1 JSP Elements ....................................................................................................... 35
3.4 JSP Application Design with MVC ........................................................................... 37
Chapter 4. Setting Up the JSP Environment................................................................... 39
4.1 Installing the Java Software Development Kit........................................................... 39
4.2 Installing the Tomcat Server ...................................................................................... 40
4.2.1 Windows Platforms ............................................................................................. 41
4.2.2 Unix Platforms (Including Linux and Mac OS X) .............................................. 43
4.3 Testing Tomcat........................................................................................................... 43
4.4 Installing the Book Examples .................................................................................... 45
4.5 Example Web Application Overview ........................................................................ 47

Table of Contents

Part II: JSP Application Development................................................................................. 49


Chapter 5. Generating Dynamic Content ........................................................................ 50
5.1 Creating a JSP Page.................................................................................................... 50
5.2 Installing a JSP Page .................................................................................................. 51
5.3 Running a JSP Page.................................................................................................... 52
5.4 Using JSP Directive Elements.................................................................................... 53
5.4.1 JSP Comments..................................................................................................... 54
5.5 Using Template Text.................................................................................................. 55
5.6 Using JSP Action Elements........................................................................................ 55
5.6.1 JSP Standard Tag Library ................................................................................... 57
Chapter 6. Using JavaBeans Components in JSP Pages ................................................ 60
6.1 What Is a Bean?.......................................................................................................... 60
6.2 Declaring a Bean in a JSP Page ................................................................................. 62
6.3 Reading Bean Properties ............................................................................................ 62
6.3.1 Using the <jsp:getProperty> Action.................................................................... 63
6.3.2 Using the JSTL Expression Language ................................................................ 64
6.3.3 Including Images with JSP.................................................................................. 65
6.4 Setting Bean Properties .............................................................................................. 65
6.4.1 Automatic Type Conversions.............................................................................. 67
Chapter 7. Using Custom Tag Libraries and the JSP Standard Tag Library ............. 68
7.1 What Is a Custom Tag Library? ................................................................................. 68
7.2 Installing a Custom Tag Library ................................................................................ 70
7.3 Declaring a Custom Tag Library................................................................................ 70
7.3.1 Identifying a Custom Tag Library in a JSP 1.1 Container .................................. 72
7.4 Using Actions from a Tag Library ............................................................................. 73
7.4.1 Setting Action Attribute Values .......................................................................... 74
7.4.2 The JSP Standard Tag Library ............................................................................ 75
7.4.3 Using Beans or Custom Actions ......................................................................... 78
Chapter 8. Processing Input and Output ......................................................................... 80
8.1 Reading Request Parameter Values ........................................................................... 80
8.1.1 Accessing Parameter Values with JSTL Actions................................................ 82
8.1.2 Accessing Other Request Data ............................................................................ 86
8.1.3 Capturing Parameter Values Using a Bean ......................................................... 89
8.2 Validating User Input ................................................................................................. 91
8.2.1 Validating User Input Using JSTL Actions ........................................................ 92
8.2.2 Validating User Input Using a Bean ................................................................... 97
8.3 Formatting HTML Output........................................................................................ 101
Chapter 9. Error Handling and Debugging................................................................... 103
9.1 Dealing with Syntax Errors ...................................................................................... 103
9.1.1 Element Syntax Errors ...................................................................................... 103
9.1.2 JSTL Expression Language Syntax Errors........................................................ 107
9.2 Debugging a JSP Application .................................................................................. 110
9.3 Dealing with Runtime Errors ................................................................................... 115
9.3.1 Catching Exceptions.......................................................................................... 119
Chapter 10. Sharing Data Between JSP Pages, Requests, and Users.......................... 121
10.1 Passing Control and Data Between Pages.............................................................. 121
10.1.1 Passing Control from One Page to Another .................................................... 122
10.1.2 Passing Data from One Page to Another......................................................... 124
10.1.3 All Together Now............................................................................................ 126

ii

Table of Contents

10.2 Sharing Session and Application Data ................................................................... 128


10.2.1 Session Tracking Explained ............................................................................ 129
10.2.2 Counting Page Hits.......................................................................................... 132
10.2.3 URL Rewriting................................................................................................ 134
10.3 Online Shopping..................................................................................................... 137
10.3.1 Number Formatting ......................................................................................... 141
10.3.2 Using a Request Parameter as an Index .......................................................... 142
10.3.3 Redirect Versus Forward................................................................................. 145
10.4 Memory Usage Considerations .............................................................................. 145
Chapter 11. Accessing a Database .................................................................................. 148
11.1 Accessing a Database from a JSP Page.................................................................. 148
11.1.1 Application Architecture Example.................................................................. 149
11.1.2 Table Example................................................................................................. 150
11.1.3 The DataSource Interface and JDBC Drivers ................................................. 151
11.1.4 Reading and Storing Information in a Database ............................................. 155
11.1.5 Generating HTML from a Query Result ......................................................... 163
11.1.6 Searching for Rows Based on Partial Information.......................................... 166
11.1.7 Deleting Database Information ....................................................................... 167
11.1.8 Displaying Database Data over Multiple Pages.............................................. 169
11.2 Validating Complex Input Without a Bean............................................................ 173
11.3 Using Transactions................................................................................................. 177
11.4 Application-Specific Database Actions ................................................................. 178
Chapter 12. Authentication and Personalization .......................................................... 180
12.1 Container-Provided Authentication........................................................................ 180
12.1.1 Authenticating Users ....................................................................................... 180
12.1.2 Controlling Access to Web Resources ............................................................ 182
12.2 Application-Controlled Authentication.................................................................. 185
12.2.1 A Table for Personalized Information............................................................. 187
12.2.2 Logging In ....................................................................................................... 187
12.2.3 Authentication Using a Database .................................................................... 190
12.2.4 Checking for a Valid Session .......................................................................... 195
12.2.5 Updating the User Profile................................................................................ 199
12.2.6 Logging Out .................................................................................................... 201
12.3 Other Security Concerns ........................................................................................ 202
Chapter 13. Internationalization .................................................................................... 203
13.1 How Java Supports Internationalization and Localization..................................... 204
13.1.1 The Locale Class ............................................................................................. 204
13.1.2 Formatting Numbers and Dates....................................................................... 206
13.1.3 Using Localized Text ...................................................................................... 206
13.2 Generating Localized Output ................................................................................. 207
13.2.1 Using One Page for Multiple Locales ............................................................. 209
13.2.2 Using a Separate Page per Locale ................................................................... 222
13.3 A Brief History of Bits ........................................................................................... 223
13.4 Handling Localized Input....................................................................................... 225
13.4.1 Dealing with Non-Western European Input.................................................... 229
Chapter 14. Working with XML Data ........................................................................... 234
14.1 Generating an XML Response ............................................................................... 234
14.2 Transforming XML into HTML ............................................................................ 236
14.3 Transforming XML into a Device-Dependent Format .......................................... 240

iii

Table of Contents

14.4 Processing XML Data ............................................................................................ 242


14.4.1 Caching Data ................................................................................................... 245
14.4.2 Parsing XML Data .......................................................................................... 245
14.4.3 Accessing XML Data Using XPath Expressions ............................................ 246
Chapter 15. Using Scripting Elements ........................................................................... 253
15.1 Using page Directive Scripting Attributes ............................................................. 253
15.2 Implicit JSP Scripting Objects ............................................................................... 254
15.3 Using Scriptlets ...................................................................................................... 256
15.4 Using Expressions .................................................................................................. 258
15.5 Using Declarations ................................................................................................. 258
15.5.1 jspInit() and jspDestroy() ................................................................................ 261
15.6 Mixing Action Elements and Scripting Elements .................................................. 262
15.6.1 Using an Expression Element to Set an Attribute ........................................... 262
15.6.2 Using JSTL with Request-Time Attribute Values .......................................... 263
15.6.3 Accessing Scoped Variables in Scripting Code .............................................. 264
15.7 Dealing with Scripting Syntax Errors .................................................................... 266
15.7.1 Scripting Syntax Error Examples.................................................................... 269
Chapter 16. Bits and Pieces ............................................................................................. 273
16.1 Buffering ................................................................................................................ 273
16.2 Including Page Fragments ...................................................................................... 275
16.3 Mixing Client-Side and Server-Side Code............................................................. 280
16.3.1 Generating JavaScript Code ............................................................................ 281
16.3.2 Using Java Applets.......................................................................................... 287
16.4 Precompiling JSP Pages ......................................................................................... 288
16.5 Preventing Caching of JSP Pages........................................................................... 291
16.6 Writing JSP Pages as XML Documents................................................................. 293
16.7 How URIs Are Interpreted ..................................................................................... 295
Part III: JSP in J2EE and JSP Component Development ............................................... 298
Chapter 17. Web Application Models ............................................................................ 299
17.1 The Java 2 Enterprise Edition Model..................................................................... 299
17.2 The MVC Design Model........................................................................................ 301
17.2.1 Using Only JSP ............................................................................................... 302
17.2.2 Using Servlets and JSP.................................................................................... 303
17.2.3 Using Servlets, JSP, and EJB .......................................................................... 304
17.3 Scalability............................................................................................................... 305
17.3.1 Preparing for Distributed Deployment ............................................................ 308
Chapter 18. Combining JSP and Servlets...................................................................... 310
18.1 Servlets, Filters, and Listeners ............................................................................... 310
18.1.1 Servlet Lifecycle ............................................................................................. 310
18.1.2 Compiling and Installing a Servlet.................................................................. 312
18.1.3 Reading a Request........................................................................................... 313
18.1.4 Generating a Response .................................................................................... 315
18.1.5 Using Filters and Listeners.............................................................................. 317
18.1.6 Sharing Data Between the Component Types................................................. 318
18.2 Picking the Right Component Type for Each Task................................................ 320
18.3 Initializing Shared Resources Using a Listener ..................................................... 322
18.4 Access Control Using a Filter ................................................................................ 324

iv

Table of Contents

18.5 Centralized Request Processing Using a Servlet.................................................... 328


18.5.1 Struts Request Processing Overview............................................................... 329
18.5.2 Mapping Application Requests to the Servlet................................................. 330
18.5.3 Dispatching Requests to an Action Class........................................................ 332
18.5.4 Implementing the Action Classes.................................................................... 334
18.5.5 Processing Requests ........................................................................................ 337
18.5.6 Calling the Controller Servlet from JSP Pages ............................................... 338
18.6 Using a Common JSP Error Page .......................................................................... 340
Chapter 19. Developing JavaBeans Components for JSP ............................................ 343
19.1 Beans as JSP Components...................................................................................... 343
19.1.1 JavaBeans Naming Conventions ..................................................................... 344
19.1.2 Compiling and Installing a Bean..................................................................... 348
19.2 JSP Bean Examples................................................................................................ 348
19.2.1 Value Beans..................................................................................................... 349
19.2.2 Utility Beans.................................................................................................... 351
19.2.3 Multithreading Considerations........................................................................ 355
19.3 Unexpected <jsp:setProperty> Behavior................................................................ 356
Chapter 20. Developing Custom Tag Libraries............................................................. 359
20.1 Tag Extension Basics ............................................................................................. 359
20.2 Developing a Simple Action .................................................................................. 362
20.3 Developing an Iterating Action .............................................................................. 365
20.4 Processing the Action Body ................................................................................... 368
20.4.1 Dealing with Empty Elements......................................................................... 373
20.5 Handling Exceptions .............................................................................................. 374
20.6 The Tag-Handler Lifecycle and What It Means to You......................................... 376
20.6.1 Providing Default Values for Optional Attributes .......................................... 377
20.6.2 Resetting Per-Invocation State ........................................................................ 378
20.6.3 Keeping Expensive Resources for the Lifetime of the Tag Handler Instance 378
20.7 Creating the Tag Library Descriptor ...................................................................... 379
20.7.1 General Library Elements ............................................................................... 380
20.7.2 Validator and Listener Elements ..................................................................... 381
20.7.3 Tag Elements................................................................................................... 381
20.7.4 Differences Between a JSP 1.1 and a JSP 1.2 TLD ........................................ 383
20.8 Packaging and Installing a Tag Library ................................................................. 383
20.8.1 Making the Tag Library Files Available to the Container .............................. 383
20.8.2 Identifying the Tag Library in a JSP Page ...................................................... 384
20.8.3 Packaging Multiple Libraries in One JAR File............................................... 385
Chapter 21. Advanced Custom Tag Library Features ................................................. 387
21.1 Developing Cooperating Actions ........................................................................... 387
21.1.1 Using Explicit Parent-Child Cooperation ....................................................... 387
21.1.2 Using Implicit Cooperation Through Variables.............................................. 390
21.2 Validating Syntax................................................................................................... 397
21.2.1 Validation Based on the TLD.......................................................................... 398
21.2.2 Using a TagLibraryValidator .......................................................................... 398
21.2.3 Using a TagExtraInfo Class for Validation..................................................... 401
21.3 Using a Listener in a Tag Library .......................................................................... 402
21.4 Dynamic Attribute Values and Types .................................................................... 403
21.4.1 Conversions Performed by the Container ....................................................... 404
21.4.2 Using a PropertyEditor for Conversion........................................................... 406

Table of Contents

Chapter 22. Integrating Custom Code with JSTL ........................................................ 408


22.1 Using the Expression Language in Custom Actions.............................................. 408
22.2 Setting and Using Configuration Variables ........................................................... 410
22.3 Integrating Custom Conditional Actions................................................................ 412
22.4 Integrating Custom Iteration Actions..................................................................... 413
22.4.1 Implementing a Custom Iteration Action........................................................ 414
22.4.2 Interacting with an Iteration Action ................................................................ 418
22.5 Integrating Custom I18N Actions .......................................................................... 420
22.6 Integrating Custom Database Actions.................................................................... 422
22.7 Using JSTL Tag Library Validators....................................................................... 423
Chapter 23. Database Access Strategies......................................................................... 426
23.1 JDBC Basics........................................................................................................... 426
23.2 Using Connections and Connection Pools ............................................................. 429
23.2.1 Using a JDBC 2.0 Optional Package Connection Pool................................... 431
23.2.2 Making a JDBC 1.0 Connection Pool Behave as a JDBC 2.0
Connection Pool .............................................................................................. 432
23.3 Making a Connection Pool Available to Application Components ....................... 437
23.3.1 Using an Application Scope Variable ............................................................. 437
23.3.2 Using JNDI...................................................................................................... 439
23.4 Using a Generic Database Bean ............................................................................. 444
23.5 Developing Application-Specific Database Components ...................................... 447
Part IV: Appendixes......................................................................................................... 451
Appendix A. JSP Elements Reference............................................................................ 452
A.1 Directive Elements .................................................................................................. 452
A.2 Scripting Elements .................................................................................................. 455
A.3 Action Elements ...................................................................................................... 457
A.3.1 Custom Actions ................................................................................................ 466
A.4 Comments................................................................................................................ 466
A.5 Escape Characters.................................................................................................... 467
Appendix B. JSTL Actions and API Reference............................................................. 468
B.1 JSTL Library URIs and Default Prefixes ................................................................ 468
B.2 Core Library Actions ............................................................................................... 468
B.3 Internationalization and Formatting Actions........................................................... 480
B.4 Database Access Actions......................................................................................... 493
B.5 XML Processing Actions......................................................................................... 499
B.6 Support and Utility Types........................................................................................ 507
B.7 Configuration Settings............................................................................................. 514
Appendix C. JSTL Expression Language Reference.................................................... 518
C.1 Syntax ...................................................................................................................... 518
C.1.1 Literals .............................................................................................................. 518
C.1.2 Keywords and Reserved Words ....................................................................... 519
C.2 Variables.................................................................................................................. 519
C.2.1 Implicit Variables ............................................................................................. 519
C.3 Data Types............................................................................................................... 520
C.3.1 Coercion Rules ................................................................................................. 520
C.4 Expressions and Operators ...................................................................................... 521
C.4.1 Operand Coercing Rules................................................................................... 522

vi

Table of Contents

Appendix D. JSP API Reference..................................................................................... 524


D.1 Implicit Variables .................................................................................................... 524
D.2 Other Servlet Types Accessible Through Implicit Variables.................................. 546
D.3 Tag Handler Types .................................................................................................. 550
D.4 Tag Library Validation Types ................................................................................. 567
D.5 Other JSP Types ...................................................................................................... 569
Appendix E. Book Example Custom Actions and API Reference............................... 575
E.1 Generic Custom Actions.......................................................................................... 575
E.2 Generic Utility Classes ............................................................................................ 584
Appendix F. Web Application Structure and Deployment Descriptor Reference..... 591
F.1 Web Application File Structure ............................................................................... 591
F.1.1 Placing Java Class Files in the Right Directory................................................ 591
F.2 Web Application Deployment Descriptor................................................................ 592
F.3 Creating a WAR File................................................................................................ 607
Colophon ........................................................................................................................... 608

To access for fully document please click here.

vii