TEAM LinG - Live, Informative, Non-cost and Genuine! 01_57485x ffirs.qxd 1/18/05 11:01 PM Page i ™ Beginning JavaServer Pages TEAM LinG - Live, Informative, Non-cost and Genuine! 01_57485x ffirs.qxd 1/18/05 11:01 PM Page ii TEAM LinG - Live, Informative, Non-cost and Genuine! 01_57485x ffirs.qxd 1/18/05 11:01 PM Page iii ™ Beginning JavaServer Pages Vivek Chopra Sing Li Rupert Jones Jon Eaves John T. Bell TEAM LinG - Live, Informative, Non-cost and Genuine! 01_57485x ffirs.qxd 1/18/05 11:01 PM Page iv Beginning JavaServer Pages™ Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2005 by Wiley Publishing, Inc., Indianapolis, Indiana Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 0-7645-7485-X Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 1B/QT/QS/QV/IN No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, e-mail: [email protected]. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY:THE PUBLISHER AND THE AUTHOR MAKE NO REP- RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACYOR COMPLETENESS OF THE CON- TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR APARTICULAR PURPOSE. NO WARRANTY MAY BE CRE- ATED OR EXTENDED BYSALES OR PROMOTIONALMATERIALS. THE ADVICE AND STRATEGIES CON- TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONALSERVICES. IF PROFESSIONALASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALLBE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS ACITATION AND/OR APOTENTIALSOURCE OF FUR- THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR- MATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAYHAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services or to obtain technical support, please contact our Cus- tomer Care Department within the U.S. at (800) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Cataloging-in-Publication Data Beginning JavaServer pages / Vivek Chopra ... [et al.]. p. cm. Includes index. ISBN 0-7645-7485-X (paper/website) 1. JavaServer pages. 2. Web sites--Design. 3. Web site development. I. Chopra, Vivek. TK5105.8885.J38B45 2005 006.7 6--dc22 2004024591 Trademarks:Wiley, the Wiley Publishing logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. JavaServer Pages is a trademark of Sun Microsystems, Inc. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. TEAM LinG - Live, Informative, Non-cost and Genuine! 01_57485x ffirs.qxd 1/18/05 11:01 PM Page v About the Authors Vivek Choprahas over ten years of experience as a software developer, architect, and team lead, with extensive experience with Web services, J2EE, and middleware technologies. He has worked and con- sulted at a number of Silicon Valley companies and startups and has (pending) patents on Web services. Vivek actively writes about technology and has coauthored half a dozen books on topics such as open- source software, Java, XML, and Web services. He contributes to open source, too, and has developed parts of the uddi4j library, an open-source Java API for UDDI. Sing Li, first bit by the microcomputer bug in 1978, has grown up with the microprocessor age. His first personal computer was a do-it-yourself Netronics COSMIC ELF computer with 256 bytes of memory, mail-ordered from the back pages of Popular Electronicsmagazine. Currently, Sing is a consultant, system designer, open-source software contributor, and freelance writer. He writes for several popular technical journals and e-zines and is the creator of the Internet Global Phone,one of the very first Internet phones available. He has authored and coauthored numerous books across diverse technical topics, including JSP, Tomcat, servlets, XML, Jini, and JXTA. Rupert Jonesis a Technical Lead for J2EE projects at Internet Business Systems. Over the past six years, Rupert has provided software development and consulting services for blue-chip companies, both in Australia and internationally. He lives and works in Melbourne, Australia. Rupert can be contacted at [email protected]. Jon Eaveshas been developing software in a variety of languages and domains for over 15 years. He is currently employed by ThoughtWorks, developing large-scale enterprise systems using J2EE. When he can find spare time, he develops J2ME/MIDPapplications and works on the BouncyCastle Crypto APIs (www.bouncycastle.org). Jon can be reached at [email protected]. John T. Bellhas more than 20 years of software development experience and currently serves as the lead software architect for the Web site of a major hospitality company based in Bethesda, Maryland. He is also an adjunct professor, teaching server-side Java technologies for the Center for Applied Information Technology at Towson State University. He has a master’s degree in Computer Systems Management and a bachelor’s degree in Electrical Engineering, both from the University of Maryland. This is Mr. Bell’s third contribution to a Wrox title. He is also the author of The J2EE Open Source Toolkit. TEAM LinG - Live, Informative, Non-cost and Genuine! 01_57485x ffirs.qxd 1/18/05 11:01 PM Page vi Credits Acquisitions Editor Project Coordinator Robert Elliott April Farling Development Editor Graphics and Production Specialists Sydney Jones Jonelle Burns Carrie A. Foster Technical Editor Lauren Goddard Wiley-Dreamtech India Pvt Ltd Denny Hager Joyce Haughey Production Editor Amanda Spagnuolo William A. Barton Quality Control Technicians Copy Editor Jessica Kramer Luann Rouff Susan Moritz Carl William Pierce Editorial Manager Charles Spencer Mary Beth Wakefield Media Development Specialist Vice President and Executive Group Publisher Kit Malone Richard Swadley Proofreading and Indexing Vice President and Publisher TECHBOOKS Production Services Joseph B. Wikert TEAM LinG - Live, Informative, Non-cost and Genuine! 01_57485x ffirs.qxd 1/18/05 11:01 PM Page vii Acknowledgments Vivek Chopra I’d like to thank my coauthors and all the folks at Wrox for the effort and the long hours—thank you, Rupert, Sing, Jon, John, Sydney, James, and Bob! I’d especially like to thank my wife, Rebecca, for her patience and support, especially since I spent most weekends working on this book. Sing Li Thanks to the virtual Beginning JavaServer Pagesteam, top professionals from all corners of the globe. Itwas wonderful working with you throughout 2003 and 2004. To my wife, Kim—your inspiration and support are the high-octane fuel that keeps me running, looking joyfully forward to each brave new day. Rupert Jones First of all, I’d like to thank my coauthors for their dedication to this book. It’s not easy holding down a full-time job in this industry and fitting in time for such a venture. I am constantly amazed we all still have friends and family who will talk to us, let alone recognize us. It’s been a pleasure undertaking this task with a group of such consummate professionals. To the Wrox crew—in particular, Sydney Jones, our developmental editor, and our tech reviewers at DreamTech—thanks for all your hard work in getting us across the line. I know its difficult working with technical people at the best of times, especially when they are geographically remote. Thanks also to my colleagues at Internet Business Systems: Steve Hayes, Rob Mitchell, and Shane Clauson. These guys provided me with much-needed help, advice, and encouragement, even in my grumpier moments. It really is a pleasure to work with each of you. And last but certainly not least, thanks to my loving family: Julia, Michael, Nick, and Caroline. Jon Eaves I’d like to thank my coauthors and the team at Wrox for the hard work they put in while creating this book. Closer to home, I’d like to thank my family for their encouragement, patience, and support. Mum and Dad, everything I can do is because of your love. My wonderful wife, Sue, you rock my world more and more every day. Boo and Maddy, your purring late at night kept me going when the words wouldn’t come. Thank you, all. John T.Bell To Tammy, my loving and patient wife, maybe someday I will write a book that you can read, and to my grandmother, Valmai Locklair, who slipped away from us as I was writing my chapters. TEAM LinG - Live, Informative, Non-cost and Genuine! 01_57485x ffirs.qxd 1/18/05 11:01 PM Page viii TEAM LinG - Live, Informative, Non-cost and Genuine! 02_57485x ftoc.qxd 1/18/05 11:01 PM Page ix Contents About the Authors v Acknowledgments vii Introduction xxv The Right Way to Do Web Development xxv Approach xxvi How This Book Is Structured xxvi Conventions xxix Source Code xxx Errata xxx p2p.wrox.com xxx Part I: JSP Fundamentals 1 Chapter 1: Getting Started with JavaServer Pages 3 Creating Applications for the Internet 4 Limitations of the basic Web server model 5 Dynamic HTML generation via CGI 5 Shortcomings of CGI 7 Improving Java-based CGI: servlets 7 Summary 28 Exercises 29 Chapter 2: JSP Basics 1: Dynamic Page Creation for Data Presentation 31 The Anatomy of a JSP Page 31 Directives 32 XML-compatible syntax 33 Template data 33 Action 34 Scripting elements 35 Handling HTML form submission with JSP 36 Web site personalization 60 Summary 70 Exercises 71 TEAM LinG - Live, Informative, Non-cost and Genuine!