You are on page 1of 10

JavaServer Pages. 2nd Edition Hans Bergsten Publisher: O'Reilly August 2002 ISBN: 0-596-00317-X. as well as thorough coverage of Custom Tag Libraries. 684 pages Filled with useful examples and the depth. . and attention to detail that made the first edition so popular with web developers.2 version of the JSP specifications. 2nd Edition is completely revised and updated to cover the substantial changes in the 1. clarity. 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.JavaServer Pages™.

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

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

..........4 Using JSP Directive Elements.........3 Using Beans or Custom Actions .............................................. 97 8..................................................... 124 10...................................4........................................................................................................................2 Debugging a JSP Application ................................................ 50 5..................3..................................................................................6.............1................... 86 8.....................................................................1 Creating a JSP Page......................................1.....2 Validating User Input ....................................................................................................................................... 49 Chapter 5................................... Generating Dynamic Content .. 121 10.................................... 57 Chapter 6......... 51 5..................................................................... 64 6.....3 Declaring a Custom Tag Library........4............................3................................................................................................................................................................................................4 Setting Bean Properties .....1 Accessing Parameter Values with JSTL Actions..................... 103 9.. 82 8..1 JSP Standard Tag Library ...................................................1 Validating User Input Using JSTL Actions .....................1......................................................... 119 Chapter 10................................................. 50 5.................................................................................................................................. 110 9.....3 Capturing Parameter Values Using a Bean ......................................................................................4 Using Actions from a Tag Library .................4............................. Using Custom Tag Libraries and the JSP Standard Tag Library .........................................................4.........................6 Using JSP Action Elements........................................................1 Identifying a Custom Tag Library in a JSP 1................................................. 73 7................... and Users.........................1 Passing Control from One Page to Another .................3 Formatting HTML Output... 126 ii ............ 70 7......2 Installing a Custom Tag Library .......................................................................................... 60 6......................................................................2..1 What Is a Bean?......2................................... 103 9... 62 6....................4.......................................... 78 Chapter 8.................................... Using JavaBeans Components in JSP Pages ........................... 68 7.................1.........3.....................1 What Is a Custom Tag Library? ............ 70 7..........1 JSP Comments.1 Element Syntax Errors ....................1 Container ............................1.............................................. Error Handling and Debugging...................3 All Together Now...........................................................3............................. 101 Chapter 9.................1 Catching Exceptions..............................3 Dealing with Runtime Errors ......................... 67 Chapter 7.......................... 75 7.............. 54 5.... Requests..........................2 Declaring a Bean in a JSP Page .........1 Dealing with Syntax Errors ........... 55 5..........................................3 Reading Bean Properties .. Sharing Data Between JSP Pages...... 103 9............................................................... 53 5.................................................... 62 6... 115 9........... 91 8... 65 6................................................................................3 Running a JSP Page.... 72 7................2 Installing a JSP Page ... 92 8....2 Accessing Other Request Data ..........................................................................................Table of Contents Part II: JSP Application Development........................................... 89 8.1 Reading Request Parameter Values ..1 Using the <jsp:getProperty> Action.......................................... 80 8................. 121 10............................................................................ 65 6...................... 74 7..................2 The JSP Standard Tag Library .................... 52 5............. 122 10.5 Using Template Text.......2 Validating User Input Using a Bean .............3........................... 107 9............................................................................................. 80 8.....................2 Passing Data from One Page to Another......................1....3 Including Images with JSP........................................................................................... 60 6.....................1 Automatic Type Conversions.....1............. 68 7..........................2 JSTL Expression Language Syntax Errors...........1 Setting Action Attribute Values ....................................... Processing Input and Output .............................1..................1 Passing Control and Data Between Pages. 63 6.......................... 55 5............2 Using the JSTL Expression Language ..................................................

......... 199 12................................. 177 11.....3........................................................................1 Authenticating Users .......2 Sharing Session and Application Data ........6 Searching for Rows Based on Partial Information................ 187 12..... 142 10....... 180 12... 173 11........................................................................................................1...............1........................................2...................... 206 13......2.............. 148 11................................................ 148 11................................................. 207 13...............................................7 Deleting Database Information ........ 240 iii ......................................4 Reading and Storing Information in a Database .3 Other Security Concerns ......................3 Authentication Using a Database .........2 Transforming XML into HTML ..5 Updating the User Profile........ Working with XML Data ..........................................................3........4....................... 225 13.................................. 222 13... 204 13....................................................1. 167 11...................................... 182 12.. 180 12...........1 Accessing a Database from a JSP Page.............................1 The Locale Class ................................... 234 14................................ 129 10..............................................2 Application-Controlled Authentication.........6 Logging Out ............................1 Dealing with Non-Western European Input............................. 150 11.......................................2..................1 How Java Supports Internationalization and Localization...................................................... 223 13..........................1...........2......................................... 195 12...............................3 Transforming XML into a Device-Dependent Format ...................................1 Generating an XML Response .......Table of Contents 10.........2 Logging In ............................................................. Authentication and Personalization .2 Counting Page Hits..................................................3 A Brief History of Bits ............ 141 10.2................................................... 209 13..................................................... 203 13.1................................3 URL Rewriting................................................................................ 187 12............................................................1.......2...........................................................................................1.... Internationalization ...................................................4 Checking for a Valid Session ....... 134 10........... 185 12...........................1...............................3 Redirect Versus Forward..................... 234 14........................................... 163 11...............................8 Displaying Database Data over Multiple Pages...........2 Using a Separate Page per Locale ...4 Handling Localized Input....... 202 Chapter 13....2........................................................................................................................... 180 12.............................................................. 206 13........ 128 10................................2.....5 Generating HTML from a Query Result ..... 166 11.............................. Accessing a Database .....2 Table Example...................................... 178 Chapter 12........................................4 Memory Usage Considerations ...........................2 Formatting Numbers and Dates.........1 Container-Provided Authentication...... 145 Chapter 11..........................................1 Using One Page for Multiple Locales ............................. 204 13.................................... 155 11........3 Using Transactions...............................1................................................3 Using Localized Text .....................1 Application Architecture Example................ 137 10......................................................................... 236 14............................... 149 11...........................1 Number Formatting .............................................................4 Application-Specific Database Actions ..........................................................3 Online Shopping..................................................................3................2 Validating Complex Input Without a Bean.............1........................................................................... 229 Chapter 14..........1.................................................................................... 169 11............................................... 151 11......................1 Session Tracking Explained ..................1 A Table for Personalized Information......... 132 10.............1.................2 Generating Localized Output ..........................................2......1..............................2....................2 Controlling Access to Web Resources ........2 Using a Request Parameter as an Index .....2....3 The DataSource Interface and JDBC Drivers ............................................................... 190 12............................................................................................ 145 10....................... 201 12............................................

..................3 Accessing Scoped Variables in Scripting Code . 318 18... 317 18...........1.................................2 Picking the Right Component Type for Each Task..................................................... JSP...............4 Using Expressions .................................................................................................................1 Using page Directive Scripting Attributes ..... and EJB .......... and Listeners ......6....................................... Filters......2 Including Page Fragments ................. 299 17............................4....................3 Reading a Request............. Web Application Models ................ 246 Chapter 15.......................................3..........................1 The Java 2 Enterprise Edition Model..2 Using JSTL with Request-Time Attribute Values .............................................5 Using Declarations . 310 18............................... Bits and Pieces ........................... 295 Part III: JSP in J2EE and JSP Component Development .....3 Scalability.1................ 313 18............. 253 15.........................................................................................4................1 Generating JavaScript Code .....................................................................................................1 Scripting Syntax Error Examples..............................................4 Generating a Response .6 Sharing Data Between the Component Types........... 242 14.....................................................................................7 How URIs Are Interpreted .................................................................. 322 18..................................................................................................... 262 15................... Combining JSP and Servlets.....2 Using Java Applets....................................................................6 Mixing Action Elements and Scripting Elements ...... 298 Chapter 17................................ 263 15..................................................... 291 16.........................................................1 Caching Data ..............1 Buffering ................. 262 15.................................................3 Mixing Client-Side and Server-Side Code.......................................................1 Using an Expression Element to Set an Attribute ...6. 269 Chapter 16.........4 Precompiling JSP Pages . 258 15..................................................................................................................................................................................... 288 16................................... 281 16.............. 261 15..........................2...............................6............................... 280 16.......................... 287 16....6 Writing JSP Pages as XML Documents.............................. 308 Chapter 18............................................... 324 iv .................................................................1 Preparing for Distributed Deployment ......................................3 Using Servlets................2.... 303 17................ Using Scripting Elements ........... 302 17....4.................................1............1........................................................... 293 16.......................................... 275 16...................................5 Preventing Caching of JSP Pages...4 Processing XML Data ..........................................................................................................................................2.... 315 18.............................5.2 Implicit JSP Scripting Objects ..........2 Using Servlets and JSP. 273 16....... 273 16..... 253 15..1..............1 Servlet Lifecycle ...................................................................................3...1.................5 Using Filters and Listeners.......................1 Servlets.....3 Accessing XML Data Using XPath Expressions ...............................................7...................................................3 Initializing Shared Resources Using a Listener .................................................................................... 299 17..... 304 17..........Table of Contents 14....3 Using Scriptlets ......................................... 305 17...........2 Parsing XML Data .......... 264 15......................1 jspInit() and jspDestroy() . 258 15...........................................2 Compiling and Installing a Servlet............... 254 15...........................................................................................................................................................................................................2 The MVC Design Model........................ 320 18......................3....................................7 Dealing with Scripting Syntax Errors ........................................... 310 18........................................... 245 14......................................... 310 18.................................. 301 17................ 256 15....... 245 14............................................................................4 Access Control Using a Filter ........................................................1 Using Only JSP ............................ 312 18.................... 266 15................

.......................1 Validation Based on the TLD............................................................................ Developing Custom Tag Libraries.2 Utility Beans................................................................3 Dispatching Requests to an Action Class....... 377 20....3 Packaging Multiple Libraries in One JAR File.................... 383 20..6.........................................1 Developing Cooperating Actions ....... 402 21............1 JavaBeans Naming Conventions ........3 Unexpected <jsp:setProperty> Behavior..1 Using Explicit Parent-Child Cooperation ............................3 Keeping Expensive Resources for the Lifetime of the Tag Handler Instance 378 20............... 328 18....3 Multithreading Considerations..........................................1 Value Beans....................................... 383 20.............................. 379 20......................5................. 398 21.................................................................... Advanced Custom Tag Library Features ............3 Using a TagExtraInfo Class for Validation..............5.........................................................1 General Library Elements ......................... 349 19....8 Packaging and Installing a Tag Library ............ 344 19.......................1 Beans as JSP Components................................................. Developing JavaBeans Components for JSP ...............................4 Processing the Action Body .................................................................. 384 20.................................................. 406 v .......................... 387 21........ 404 21.................5.............................2 Compiling and Installing a Bean..................2 Developing a Simple Action ......................... 401 21................................................8........................................................3 Tag Elements.............................1 and a JSP 1............................................................................................................................................7 Creating the Tag Library Descriptor ........ 390 21......................... 374 20...........1.............................................Table of Contents 18................3 Developing an Iterating Action ............................................2 Validator and Listener Elements ..........................3 Using a Listener in a Tag Library ....... 380 20.............................................2..................................................................5 Processing Requests ..1............................5......................... 373 20...... 355 19......................................................1..........1 Dealing with Empty Elements..............7......1 Tag Extension Basics ...................... 381 20......1 Making the Tag Library Files Available to the Container .........2 TLD .......................................5....... 337 18. 348 19.......................... 348 19.... 381 20.................................................5 Handling Exceptions ....................2.....................6 The Tag-Handler Lifecycle and What It Means to You. 387 21.........................2.................................6.................7........ 356 Chapter 20.....2....2 Using a PropertyEditor for Conversion.............6 Calling the Controller Servlet from JSP Pages .......................... 343 19. 340 Chapter 19..... 359 20............................................................................2 Identifying the Tag Library in a JSP Page ........................................5..........4.............................................................. 368 20............................................................ 330 18.............................................4. 398 21.2 Using a TagLibraryValidator .....2 JSP Bean Examples.............................1.............. 365 20............................6..................................................................................................................................................................................................... 397 21........4 Implementing the Action Classes.2....6 Using a Common JSP Error Page ......2 Mapping Application Requests to the Servlet.4 Differences Between a JSP 1................... 387 21............. 329 18. 385 Chapter 21........ 359 20......8............................................................5 Centralized Request Processing Using a Servlet.............................1 Providing Default Values for Optional Attributes ..........2 Using Implicit Cooperation Through Variables.................. 332 18................................ 378 20..8........................................................... 403 21...................... 338 18.............................7........ 343 19.................................... 362 20........2 Resetting Per-Invocation State .......2 Validating Syntax................. 376 20.................................................1 Conversions Performed by the Container .............................. 334 18........................................... 383 20........4........1 Struts Request Processing Overview..................................................................................................4 Dynamic Attribute Values and Types ..........2......... 351 19...........................................7...........

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

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