Академический Документы
Профессиональный Документы
Культура Документы
Vivek Chopra
Sing Li
Rupert Jones
Jon Eaves
John T. Bell
Beginning JavaServer Pages
Beginning JavaServer Pages
Vivek Chopra
Sing Li
Rupert Jones
Jon Eaves
John T. Bell
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, MA 01923, (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: brandreview@wiley.com.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP-
RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CON-
TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT
LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE-
ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. 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 PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A
COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE
AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION
OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE 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 MAY HAVE
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.
About the Authors
Vivek Chopra has 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 Electronics magazine. 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 Jones is 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
rup@rupertjones.com.
Jon Eaves has 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/MIDP applications and works on the BouncyCastle Crypto APIs
(www.bouncycastle.org). Jon can be reached at jon@eaves.org.
John T. Bell has 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 masters degree in Computer Systems Management
and a bachelors degree in Electrical Engineering, both from the University of Maryland. This is Mr.
Bells third contribution to a Wrox title. He is also the author of The J2EE Open Source Toolkit.
Credits
Acquisitions Editor Project Coordinator
Robert Elliott April Farling
Sing Li
Thanks to the virtual Beginning JavaServer Pages team, top professionals from all corners of the globe.
It was wonderful working with you throughout 2003 and 2004.
To my wife, Kimyour 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, Id like to thank my coauthors for their dedication to this book. Its 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. Its been a pleasure undertaking this
task with a group of such consummate professionals.
To the Wrox crewin particular, Sydney Jones, our developmental editor, and our tech reviewers at
DreamTechthanks 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
Id like to thank my coauthors and the team at Wrox for the hard work they put in while creating this
book. Closer to home, Id 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
wouldnt 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.
Contents
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
x
Contents
Coercion to a character 158
Best attempt to do the right thing without error 158
Accessing Object Properties and Collections 162
Implicit EL Objects in JSP 2.0 167
User-Supplied Functions within EL 173
Namespace and EL functions 173
Static methods of a Java class 173
Summary 177
Exercises 178
xi
Contents
The include Directive 231
The include performed at translation time 231
Summary 239
Exercises 239
xii
Contents
Summary 328
Exercises 328
Chapter 11: Building Your Own Custom JSP Tag Library 329
What Is a Tag File? 329
A Simple Tag File: Displaying Todays Date 330
Advantages of Tag Files 331
Code reuse 331
Hiding complexity 331
Separation of concerns 331
Simplicity 332
Flexible packaging 332
Developing Tag Files 332
Scope and implicit objects 332
Using directives in tag files 335
Body processing 341
Attributes 345
Packaging Tag Files 355
Java custom actions versus tag file custom actions 358
Summary 359
Exercises 359
xiii