Web Development with JavaServer Pages Web Development with JavaServer Pages SECOND EDITION DUANE K. FIELDS MARK A. KOLB SHAWN BAYERN MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books, go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661-9018 Greenwich, CT 06830 email: orders@manning.com ©2002 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Library of Congress Cataloging-in-Publication Data Manning Publications Co. Copyeditor: Elizabeth Martin 209 Bruce Park Avenue Typesetter: Tony Roberts Greenwich, CT 06830 Cover designer: Leslie Haimes Printed in the United States of America 12345678910– VHG – 04 03 02 01 To Kris— for her patience, encouragement and good humor that made this project possible D.K.F. For Megan, Andrew, and Jean— your presence is my strength, and your love my inspiration M.A.K. To my parents— For teaching me everything I know (except JSP) S.B. 1 ■ ■ ■ ■ Introduction 1 2 ■ ■ ■ ■ HTTP and servlets 17 3 ■ ■ ■ ■ First steps 30 4 ■ ■ ■ ■ How JSP works 46 5 ■ ■ ■ ■ Programming JSP scripts 65 6 ■ ■ ■ ■ Actions and implicit objects 101 7 ■ ■ ■ ■ Using JSP components 129 8 ■ ■ ■ ■ Developing JSP components 165 9 ■ ■ ■ ■ Working with databases 198 10 ■ ■ ■ ■ Architecting JSP applications 229 11 ■ ■ ■ ■ An example JSP project 272 12 ■ ■ ■ ■ Introducing filters and listeners 318 13 ■ ■ ■ ■ Applying filters and listeners 334 14 ■ ■ ■ ■ Deploying JSP applications 384 15 ■ ■ ■ ■ Performing common JSP tasks 418 16 ■ ■ ■ ■ Generating non-HTML content 470 brief contents viii BRIEF CONTENTS 17 ■ ■ ■ ■ JSP by example 493 18 ■ ■ ■ ■ Creating custom tags 529 19 ■ ■ ■ ■ Implementing advanced custom tags 582 20 ■ ■ ■ ■ Validating custom tag libraries 621 A ■ ■ ■ ■ Changes in the JSP 1.2 API 669 B ■ ■ ■ ■ Running the reference implementation 676 C ■ ■ ■ ■ Incorporating Java applets 683 D ■ ■ ■ ■ JSP resources 697 E ■ ■ ■ ■ JSP syntax reference 702 F ■ ■ ■ ■ JSP API reference 718 preface to the second edition xxv preface to the first edition xxix acknowledgments xxxi about this book xxxiii about the authors xxxviii authors online xxxix about the cover illustration xl 1 Introduction 1 1.1 What is JSP? 2 1.2 Dynamic content on the web 2 Why dynamic content? 3 ■ Common Gateway Interface 4 ■ Template systems 5 ■ Java on the Web 8 ■ How XML fits in 11 1.3 The role of JSP 13 The JavaBeans component architecture 13 JSP and Java 2 Platform Enterprise Edition 15 contents x CONTENTS 2 HTTP and servlets 17 2.1 The Hypertext Transfer Protocol (HTTP) 18 HTTP basics 18 ■ GET versus POST 21 2.2 Java servlets 23 How a web server uses servlets 24 ■ The anatomy of a servlet 24 ■ A servlet example 26 3 First steps 30 3.1 Simple text 31 3.2 Dynamic content 32 Conditional logic 33 ■ Iteration 34 Non-HTML output 37 3.3 Processing requests and managing sessions 38 Accessing request parameters 38 ■ Using sessions 39 3.4 Separating logic from presentation 41 Reusing logic with JavaBeans 42 Abstracting logic with custom tags 44 3.5 Review of examples 45 4 How JSP works 46 4.1 The structure of JSP pages 47 Directives and scripting elements 47 Standard and custom actions 48 4.2 Behind the scenes 52 Translation to servlets 52 ■ Translation versus execution 54 4.3 What the environment provides 56 Automatic servlet generation 56 ■ Buffered output 57 Session management 59 ■ Exception handling 63 Implicit objects 64 ■ Support for JavaBeans and HTML forms 64 [...]... process Without them this book could not have been possible about this book JavaServer Pages is a technology that serves two different communities of developers Page designers use JSP technology to add powerful dynamic content capabilities to web sites and online applications Java programmers write the code that implements those capabilities behind the scenes Web Development with JavaServer Pages is... coverage of HTTP, the protocol that web browsers and web servers use to xxxiv ABOUT THIS BOOK communicate with one another, and Java servlets, the foundational technology for server-side Java applications The topics covered are as follows: Chapter 1 introduces JavaServer Pages (JSP) and presents a brief history of web development JSP is contrasted with past and present web technologies Since this chapter... edition When the first edition of Web Development with JavaServer Pages was published some eighteen months ago, URLs ending with a jsp file extension were a novelty Today, this is a commonplace occurrence for millions of web surfers JSP has been widely adopted, and we are very pleased to have played a supporting role in its popularization We are likewise very pleased with the reception of the first edition... second edition will prove itself a worthy successor to the original Web Development with JavaServer Pages And we look forward to uncovering even more jsp file extensions as we surf the web, hunting for the next generation of killer web applications preface to the first edition In late 1998 we were asked to develop the architecture for a new web site Our employer, a vendor of enterprise software for system... overview 218 I 10 Architecting JSP applications 229 10.1 Web applications 230 Web application flow 232 Architectural approaches 233 10.2 Page-centric design 233 Role-based pages 233 Managing page flow with action targets 236 Building composite pages 238 Limitations of the page-centric approach 241 I I 10.3 Servlet-centric design 242 Hello, World with servlets 243 JSP and the servlet API 244 Servlets... markup language It is assumed that the reader is sufficiently familiar with HTML to follow the examples presented It is likewise assumed that the reader is familiar with URLs, document hierarchies, and other concepts related to creating and publishing web pages We also do not include a primer on the Java programming language As with HTML, there is a wealth of reference information available on the... application servers with full JSP support Tool support is another area that has thankfully experienced significant growth Today, xxvi PREFACE TO THE SECOND EDITION web developers can take advantage of a wide array of editors, IDEs, and code generators with built-in support for JSP As with any Internet technology though, JSP continues to evolve In September 2001 Sun released the JavaSer ver Pages 1.2 and... applications We examine the various architectural options available when we combine JSP pages with servlets, Enterprise JavaBeans, HTML, and other software elements to create web- based applications In chapter 11 we apply the JSP programming techniques we covered in previous chapters to the development of a real world, enterprise web application In a chapterlength example, we will be developing a system for... experiment with the book’s examples Java applets are small applications that run within the context of a web browser Appendix C describes the action, a cross-platform tag for specifying applets which use Sun Microsystems’s Java Plug-in technology in order to take advantage of the Java 2 platform within the browser This appendix is directed at Java programmers As is the case with any major... 335 13.2 User authentication 337 User account representation 337 User management interface 338 User management implementation 339 I I 13.3 Web authentication 341 Session interactions 341 Login servlet 344 Login pages 350 Content pages 353 Logout servlet 357 Logout pages 358 I I I 13.4 13.5 13.6 Access control filters 360 Authentication filter 361 I Role filter Logging listener 368 HttpSessionListener . Web Development with JavaServer Pages Web Development with JavaServer Pages SECOND EDITION DUANE K. FIELDS MARK. 10.1 Web applications 230 Web application flow 232 Architectural approaches 233 10.2 Page-centric design 233 Role-based pages 233 ■ Managing page flow with