MANNING
Neal Ford
STRUTS
TAPESTRY
COMMONS
VELOCITY
JUNIT
AXIS
COCOON
INTERNETBEANS
WEBWORK
ART
OF
JAVA WEB
DEVELOPMENT
Art of Java
Web Development
Art of Java
Web Development
STRUTS, TAPESTRY, COMMONS, VELOCITY, JUNIT,
AXIS, COCOON, INTERNETBEANS, WEBWORK
NEAL FORD
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
■ Creating custom JSP tags 61
4
■ The Model 2 design pattern 91
PART II WEB FRAMEWORKS 131
5 ■ Using Struts 133
6
■ Tapestry 159
7
■ WebWork 199
8
■ InternetBeans Express 227
9
■ Velocity 261
10
■ Cocoon 283
11
■ Evaluating frameworks 311
viii BRIEF CONTENTS
PART III BEST PRACTICES 327
12 ■ Separating concerns 329
13
■ Handling flow 371
14
■ Performance 409
15
■ Resource management 445
16
■ Debugging 475
17
■ Unit testing 521
Business rules 20
■
Where should the rules reside? 22
Leveraging best practices 24
1.5 Summary 25
x CONTENTS
2
Building web applications 27
2.1 Building web applications with servlets 29
The eMotherEarth servlet application 29
Evaluating the servlet approach 50
2.2 Building web applications with JSP 50
The JSP eMotherEarth application 51
Evaluating the JSP approach 59
2.3 Summary 60
3
Creating custom JSP tags 61
3.1 The case for custom tags 62
3.2 The tag interfaces 63
The Tag interface 63
■
The IterationTag interface 64
The BodyTag interface 65
3.3 Building simple tags 66
The HtmlSqlResult tag 66
■
Registering the tag 71
3.4 Validating tag attributes 75
Adding DbPool to the application tag 75
3.5 Using prebuilt tags 80
■
Using Struts’ custom tags
to simplify JSP 142
■
Internationalization with Struts 145
Struts’ support for data entry 147
■
Declarative validations 151
5.2 Evaluating Struts 156
5.3 Summary 157
6
Tapestry 159
6.1 Overview 160
6.2 The architecture 160
6.3 A simple Tapestry application 162
Tapestry Hello, World 162
6.4 The Tapestry framework 167
Framework classes and interfaces 167
■
Components 170
6.5 Scheduling in Tapestry 173
Bootstrapping the application 173
■
The Home page 176
The custom table component 180
■
The Add page 185
6.6 Evaluating Tapestry 192
Documentation and samples 192
■
InternetBeans Express 227
8.1 Overview 228
8.2 The architecture 230
DataExpress 230
■
InternetBeans Express 233
8.3 InternetBeans Express components 234
ixPageProducer 234
■
ixComponents 236
8.4 Scheduling with InternetBeans 237
Data connectivity 238
■
The View page 242
■
The Add
page 245
■
Validations 249
8.5 JSP custom tags 255
8.6 Evaluating InternetBeans Express 257
Documentation and samples 257
Using InternetBeans Express 258
8.7 Summary 259
9
Velocity 261
9.1 Overview 262
9.2 The architecture 263
9.3 Key concepts 265
Setting up Velocity 265
■
The action 304
■
The view 305
10.5 Evaluating Cocoon 307
Documentation and samples 307
■
Source code 308
Debugging 308
10.6 Summary 309
11
Evaluating frameworks 311
11.1 Evaluation criteria 312
Suitability to the application 312
■
Documentation 315
Source code 316
■
Tool support 317
■
External criteria 318
11.2 Design considerations 319
Adherence to good design principles 319
■
The user
interface 320
■
Innovative features 321
Insularity 322
■
12.5 Summary 368
13
Handling flow 371
13.1 Application usability options 372
Building the base: eMotherEarth.com 372
■
Page-at-a-time
scrolling 378
■
Sortable columns 384
User interface techniques in frameworks 389
13.2 Building undo operations 390
Leveraging transaction processing 391
■
Using the Memento
design pattern 394
■
Undo in frameworks 401
13.3 Using exception handling 401
The difference between technical and domain exceptions 401
Creating custom exception classes 402
■
Where to catch and
handle exceptions 403
■
Exceptions in frameworks 406
13.4 Summary 407
14
Performance 409
14.1 Profiling 410
the Façade design pattern 453
■
Resource management
in frameworks 469
15.2 Other resources you need to manage 470
Effectively using JNDI 470
■
Using lazy instantiation 472
Working with web collections 472
15.3 Summary 473
16
Debugging 475
16.1 Debugging web applications 476
16.2 Debugging with the SDK 483
Starting the debugger 483
■
Running the debugger 486
Breakpoints and steps 489
■
Accessing variables 490
Effectively using jdb 492
16.3 Debugging with IDEs 493
Debugging with NetBeans 493
■
Debugging with JBuilder 498
Differences between debuggers 502
16.4 Evaluating debuggers 505
16.5 Debugging in frameworks 506
Struts 506
■
Test suites 529
■
Testing boundaries 530
■
Tool support 534
17.3 Web testing with JWebUnit 536
JWebUnit TestCases 537
■
Testing complex elements 539
17.4 Summary 541
18
Web services and Axis 543
18.1 Key concepts 544
18.2 Axis 545
Architecture of Axis 546
■
Axis tools 547
18.3 Calling web services 551
18.4 eMotherEarth web services 553
Configuration 553
■
Orders 556
■
Calling the web service 559
18.5 Summary 562
19
What won’t fit in this book 563
19.1 Persistence 564
Plain old Java objects 564
■
explains specific, practical techniques for improving your web applications.
Most development books today fall into one of two categories:
API or best prac-
tices. The
API books focus on a single API, either from J2EE and Java or, for exam-
ple, an open-source project. A perfect example is Manning’s excellent Struts in
Action, by Ted Husted et al. It takes you through everything you need to know
xviii PREFACE
about how to use Struts. The best (or worst) practices books focus on individual
topics, examining design patterns and coding samples that represent the best (or
worst) ways to perform a certain task. Art of Java Web Development overlaps some of
the topics from these other types of books, but it does so in a synergistic manner,
discussing how all these pieces (and others) combine to create real-world web
applications.
xix
acknowledgments
Writing any book is a daunting task, and the nature of this book made it even
more so. This means that my supporting structure (i.e., my family and friends)
suffered with and supported me even more than usual. For that, they have my
undying gratitude. First, to all my immediate and extended family, thanks for all
your support, especially my mother, Hazel, who bears the most responsibility for
who I am today. Also, thanks to my dad, Geary, along with Sherrie, Elisha, and the
whole menagerie for their support. I would also like to thank Lloyd, Michelle,
John, Madison, and Max (a force of nature) for all their fun and companionship,
along with Mechelle, Mark, Wyatt, and Wade. The whole Shephard clan deserves
a nod, because they care a lot more about me learning to cook the secret family
recipe for Death by Candied Yams than what I put to paper.
I would also like to thank my surrogate family here in Atlanta, as fine a bunch
of people as you will ever meet: Margie, Wright, Melissa, Julie, Walker, Jim, Randy,
and Karen. They have taken Candy and me into their family and made us feel like
Erik Hatcher, Shahram Khorsand, Howard Lewis Ship, Steve Loughran, Ted
Neward, Eitan Suez, and Luigi Viggiano. I appreciate their insights, comments,
criticisms, and feedback.
It is virtually impossible to exist in this field if you don’t have activities that fall
completely outside the technical realm. For that I have other circles of friends,
who are vaguely aware of what I do for a living, but frankly could care less. These
include my neighbors, Jamie, Diane, Kitty, and Gail. Another large support group
consists of all my triathlete buddies, who only know me as the slow guy behind
them: Jon, Joan, Jane, and Robert all fall into that group of people who help keep
me sane.
There aren’t many people who span all the above groups (plus some other
groups that I didn’t even mention). In fact, there is really only one: Terry, who
deserves special thanks for support and friendship, who is a good travel partner,
geek, and Tri-geek. And thanks to Stacy for letting him do all that stuff.
Last but certainly not least is the person who both likes and dislikes this book
the most. My beautiful and wonderful wife, Candy, whom I love more than any-
thing, has spent far too long in the company of only Winston and Parker and
deserves more of my time. Honey, this book is finally done, and I’m all yours again.
xxi
about the book
This book is for every Java web developer, regardless of his or her level of exper-
tise. It is designed primarily for intermediate to advanced developers, who under-
stand the specifics of the various web
APIs in Java but haven’t yet mastered the best
way to apply them. It is perfect for developers who have heard terms like Model-
View-Controller and Model 2, but weren’t present for the series of events that led
to the widespread adoption of these best practices. It is also perfect for designers
and architects of web applications because it discusses the implications of archi-
tecture and design at every opportunity.
This book is also well suited to developers who have looked at (and possibly
The selection of the correct framework is only the beginning of the life cycle of
an application. Part 3 examines best practices, including sophisticated user inter-
face techniques, intelligent caching and resource management, performance tun-
ing, debugging, testing, and web services.
Part 1
Chapter 1 serves as the jumping-off point for the book. It highlights all the topics
to come in the subsequent chapters and explains my primary motivation for writ-
ing the book.
Chapter 2 begins our discussion of the evolution of web applications. The idea
behind this chapter is to present an application built by a developer who is very
good with Java and understands the web
APIs but hasn’t yet applied best practices
and architecture. The first pass at the application uses only servlets (which was the
only tool available when the web
APIs first debuted). Then we build the same
application using just
JSP. In both cases, we highlight the strengths and weak-
nesses of the resulting applications.
Chapter 3 carries the evolution a step further with custom tags. It takes the
JSP
application built in the second chapter and improves it using custom JSP tags.
Chapter 4 represents the culmination of the evolution of architecture and
design. Here, we rewrite our sample application as a Model 2 application. You’ll
also learn how to leverage design patterns to improve the Model 2 application.
ABOUT THE BOOK xxiii
Part 2
Part 2 covers six web frameworks. In chapter 5, you’ll learn about Struts. We intro-
duce this framework in chapter 1, but here we “deconstruct” it and describe all
the important moving parts. Chapter 6 examines Tapestry, another Model 2–based
open-source framework. We show you how the Tapestry
SDK-supplied and commercial tools.
Next, we look at performance pitfalls and common mistakes and offer solutions.
Then we delve into object pooling and explain how to implement it using either
Java references or Jakarta Commons pooling.
Chapter 15 complements the previous chapter by showing you how to conserve
resources. We examine several sophisticated caching techniques using both the