JSP: The Complete Reference - Yola

Transcription

JSP: The Complete Reference

About the Author.Phil Hanna has more than 20 years experienceas a programmer, systems architect, analyst, andproject manager. He has developed network-basedsoftware at IBM, and served as a consultant toChase Manhattan Bank. He is the author ofInstant Java Servlets, and works as a softwaredeveloper for SAS Institute.Copyright 2001 The McGraw-Hill Companies, Inc. Click Here for Terms of Use.

JSP: The CompleteReferencePhil HannaOsborne/McGraw-HillNew York Chicago San FranciscoLisbon London Madrid Mexico CityMilan New Delhi San JuanSeoul Singapore Sydney Toronto

Copyright 2001 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or byany means, or stored in a database or retrieval system, without the prior written permission of the publisher.0-07-219431-6The material in this eBook also appears in the print version of this title: 0-07-212768-6.All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps.McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporatetraining programs. For more information, please contact George Hoare, Special Sales, at george hoare@mcgraw-hill.com or (212)904-4069.TERMS OF USEThis is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to thework. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store andretrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative worksbased upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Yourright to use the work may be terminated if you fail to comply with these terms.THE WORK IS PROVIDED “AS IS”. McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIESAS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THEWORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OROTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITEDTO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and itslicensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation willbe uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, erroror omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for anyindirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, evenif any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.DOI: 10.1036/0072194316

To Mary

This page intentionally left blank.

Contents at a GlancePart I The Web Programming Environment12345The Web Marketplace . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Evolution of the Web Application . . . . . . . . . . . . . . . . . . .Overview of the Hypertext Transfer Protocol (HTTP) . . . .Introduction to Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . .JSP Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35112353Part II Elements of JSP67891011JSP Syntax and Semantics . . . . . . . . . . . . . . . . . . . . . . . . . .Expressions and Scriptlets . . . . . . . . . . . . . . . . . . . . . . . . .Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Request Dispatching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Page Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .JSP Tag Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .viiCopyright 2001 The McGraw-Hill Companies, Inc. Click Here for Terms of Use.6389111133161183

viiiJSP: The Complete ReferencePart III JSP in Action1213141516171819HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Session and Thread Management . . . . . . . . . . . . . . . . . . .JSP and JavaBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .JSP and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .JSP Testing and Debugging . . . . . . . . . . . . . . . . . . . . . . . .Deploying Web Applications . . . . . . . . . . . . . . . . . . . . . . .Case Study: A Product Support Center . . . . . . . . . . . . . .261291353415451493529551Part IV JSP and Other Web Components20 Communicating with Other Clients . . . . . . . . . . . . . . . . .21 Communicating with Other Servers . . . . . . . . . . . . . . . . .679703Part V AppendixesA Servlet API Version 2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . .B JSP API Version 1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .C HTTP Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .723793833845

ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xixxxvPart IThe Web Programming Environment1The Web Marketplace . . . . . . . . . . . . . . . . . . . . . . . . . . . .32Evolution of the Web Application . . . . . . . . . . . . . . . . . .5Birth of the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Growth of the Web Programming Model . . . . . . . . . . . . . . . . . . . . .The Shift from Client-Side to Server-Side Solutions . . . . . . . . . . . . .669Overview of the Hypertext Transfer Protocol (HTTP) . . .11What Is HTTP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Language for Requesting Documents overthe Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The HTTP Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123ixCopyright 2001 The McGraw-Hill Companies, Inc. Click Here for Terms of Use.1212

xJSP: The Complete Reference45HTTP Request Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Connecting to the Web Server . . . . . . . . . . . . . . . . . . . . . . . .Sending the HTTP Request . . . . . . . . . . . . . . . . . . . . . . . . . .Server Acceptance of the Request . . . . . . . . . . . . . . . . . . . . .The HTTP Response from the Server . . . . . . . . . . . . . . . . . .Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13141516161721Introduction to Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . .23Servlet Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .destroy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Example: Kilometers per Liter to Miles per Gallon Servlet . . . . . . .Servlet Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Servlet Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Servlet Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Servlet Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Threading Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .SingleThreadModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HTTP Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The HttpSession Interface . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242526272731323641444749495052JSP Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53How JSP Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Basic Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5456Part IIElements of JSP6JSP Syntax and Semantics . . . . . . . . . . . . . . . . . . . . . . . . .63The JSP Development Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Components of a JSP Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Scriptlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64656568697071

Contents78Implicit Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Standard Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Tag Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Complete Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Page Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A jsp:include Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Scriptlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .JSP Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74757878838384858687Expressions and Scriptlets . . . . . . . . . . . . . . . . . . . . . . . .89Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Scriptlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Expression and Scriptlet Handling by the JSP Container . . . . . . . .HTML Template Data and Expressions . . . . . . . . . . . . . . . .Scriptlet Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Container-Generated Initialization and Exit Code . . . . . . .Implicit Objects and the JSP Environment . . . . . . . . . . . . . . . . . . . . .Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .PageContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Initialization Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . arations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111What Is a Declaration? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Where Declaration Code Is Generated . . . . . . . . . . . . . . . . .Primary Uses for Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Variable Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Thread Safety and Instance Variables . . . . . . . . . . . . . . . . .Method Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Overriding jspInit and jspDestroy . . . . . . . . . . . . . . . . . . . .Access to Implicit Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112112117117118122126128128xi

xiiJSP: The Complete Reference91011Request Dispatching . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133Anatomy of Request Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Including Other Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The include Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Effect of Changes in an Included File . . . . . . . . . . . . . . . . . .Using the include Directive to Copy Source Code . . . . . . .The jsp:include Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Which Method to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Forwarding Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The RequestDispatcher Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Request Dispatching vs. Redirection . . . . . . . . . . . . . . . . . .Model 1 vs. Model 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134136136137138139140141153154158159159160The Page Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .extends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Required Interfaces for a JSP Superclass . . . . . . . . . . . . . . .A JSP Superclass Example . . . . . . . . . . . . . . . . . . . . . . . . . . .import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .buffer and autoFlush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .isThreadSafe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .contentType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .errorPage and isErrorPage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162164164166170172172173174174176181JSP Tag Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183Why Custom Tags? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Developing Your First Custom Tag . . . . . . . . . . . . . . . . . . . . . . . . . .Step 1—Define the Tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Step 2—Create the TLD Entry . . . . . . . . . . . . . . . . . . . . . . . .Step 3—Write the Tag Handler . . . . . . . . . . . . . . . . . . . . . . .Step 4—Incorporate the Tag into a JSP Page . . . . . . . . . . . .How Tag Handlers Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What the JSP Container Does . . . . . . . . . . . . . . . . . . . . . . . . .What a Tag Handler Does . . . . . . . . . . . . . . . . . . . . . . . . . . .Tag Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Tag Library Descriptor (TLD) . . . . . . . . . . . . . . . . . . . .The taglib Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184186186187188192194194196197197199

ContentsThe Tag Handler API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Tag Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The TagSupport Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Tag Handler Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .An Example of Generated Code . . . . . . . . . . . . . . . . . . . . . .Defining Tag Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Body Tag Handler API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .BodyContent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The BodyTag Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The BodyTagSupport Class . . . . . . . . . . . . . . . . . . . . . . . . . .The Body Tag Handler Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Defining Scripting Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The TagExtraInfo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Validating Tag Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . .Cooperating Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using Syntactic Scoping . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Implementation of the DatabaseQuery Example . . . . . . . . . . . . . . .The Necessary Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Tag Library Descriptor . . . . . . . . . . . . . . . . . . . . . . . . . .The Tag Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36236247247248249258Part IIIJSP in Action1213HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261The FORM Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Attributes of the FORM Element . . . . . . . . . . . . . . . . . . . . . .Form Input Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Elements Created with the INPUT Tag . . . . . . . . . . . . . . . .Elements Created with select and option . . . . . . . . . . . . . . .The textarea Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Contact Us Form with Validation . . . . . . . . . . . . . . . . .The Server Side of Forms Handling . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263264268270283284285285287289Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291Overview of JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Basic JDBC Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .292293xiii

xivJSP: The Complete Reference14Essential JDBC Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Simple JDBC Example . . . . . . . . . . . . . . . . . . . . . . . . . . . .JDBC Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Driver Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The JDBC-ODBC Bridge . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Registering a Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Connecting to a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The JDBC Database URL . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Statement Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .PreparedStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CallableStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Scrollable Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Updatable Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .RowSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Database Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ResultSetMetadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .New Features in JDBC 2.0 and Beyond . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38339349351351Session and Thread Management . . . . . . . . . . . . . . . . . .353Session Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Hidden Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .URL Rewriting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Session API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Storing and Retrieving Objects from Sessions . . . . . . . . . . .Destroying Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Examples Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Session Binding Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . .Thread Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Threading Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Servlet Threading Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Default Threading Model . . . . . . . . . . . . . . . . . . . . . . . . . . . .Single Threaded Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Multithreaded Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Application Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11413

Contents15161718JSP and JavaBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .415What Is a JavaBean? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Bean Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .JSP Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . jsp:useBean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . jsp:setProperty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . jsp:getProperty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Complete Example—Personalization with Beans . . . . . . . . . . . .Getting Weather Data from the Web . . . . . . . . . . . . . . . . . .The LyricNote Portal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .416416419424425430434434435444448JSP and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .451XML Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Problem XML Solves . . . . . . . . . . . . . . . . . . . . . . . . . . . .XML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Document Type Definition . . . . . . . . . . . . . . . . . . . . . . .XML Parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Document Object Model (DOM) . . . . . . . . . . . . . . . . . . . . . .Simple API for XML (SAX) . . . . . . . . . . . . . . . . . . . . . . . . . .XSL Transformations with XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XSLT in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .452453453454457458472486487491JSP Testing and Debugging . . . . . . . . . . . . . . . . . . . . . . .493Building a Mental Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Translation and Compilation . . . . . . . . . . . . . . . . . . . . . . . . .Testing in Isolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Debugging Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Capturing Form Parameters . . . . . . . . . . . . . . . . . . . . . . . . .A Debugging Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . .Tracing HTTP Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .494494499500501504510528Deploying Web Applications . . . . . . . . . . . . . . . . . . . . . .529The Web Application Environment . . . . . . . . . . . . . . . . . . . . . . . . . .Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Resource Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Servlet Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Web Archive (war) File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .530530532535535xv

xviJSP: The Complete Reference19The Deployment Descriptor—web.xml . . . . . . . . . . . . . . . . . . . . . . .Sample Deployment Descriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .538547549Case Study: A Product Support Center . . . . . . . . . . . . .551Process Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Developing the System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Model-View-Controller Architecture . . . . . . . . . . . . . . . . . . . . . . . . .Model Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .View Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Controller Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .552554555556557631658676Part IVJSP and Other Web Components2021Communicating with Other Clients . . . . . . . . . . . . . . . .679URL Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The URL Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The URLConnection Class . . . . . . . . . . . . . . . . . . . . . . . . . . .The HttpURLConnection Class . . . . . . . . . . . . . . . . . . . . . . .Java Applications as Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The JSP Price Quote Server . . . . . . . . . . .

JSP: The Complete Reference Phil Hanna Osborne/McGraw-Hill New York Chicago S