|
The first Internet revolution was all about delivering information to people. We are now in the second revolution, which focuses on delivering information to systems. XML is the tool that makes this new revolution a reality, and Web services are the methods by which businesses will drive system-to-system communication. JSP(TM) and XML takes you beyond the basics, giving you practical advice and in-depth coverage. In the book, you'll learn the technologies and techniques needed to create your own Web services for use in JSP applications. Written by programmers for programmers, the book will help you successfully utilize these exciting technologies with minimal hassle and maximum speed. 777 |
Copyright | |||||||||||||||||
About the Authors | |||||||||||||||||
Acknowledgments | |||||||||||||||||
Introduction | |||||||||||||||||
On XML | |||||||||||||||||
On Web Services | |||||||||||||||||
On the Structure of the Book | |||||||||||||||||
A Word about Source Code | |||||||||||||||||
How to Use the Book | |||||||||||||||||
Conventions Used in This Book | |||||||||||||||||
A Final Note | |||||||||||||||||
Part I. Data, XML, and Web Services Introduction | |||||||||||||||||
Chapter 1. Integrating JSP and Data | |||||||||||||||||
Using JSP with a Database | |||||||||||||||||
Other Considerations | |||||||||||||||||
Basic Design Concepts | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 2. Introduction to XML/XSL | |||||||||||||||||
What Is XML? | |||||||||||||||||
Rules of XML | |||||||||||||||||
On to Using XML | |||||||||||||||||
XSL | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 3. Understanding Web Services | |||||||||||||||||
What Is a Web Service? | |||||||||||||||||
Crystal Ball Readings | |||||||||||||||||
The ABCs of Web Services | |||||||||||||||||
How to Use a Web Service | |||||||||||||||||
Summary | |||||||||||||||||
Part II. Integrating JSP and XML | |||||||||||||||||
Chapter 4. A Quick Start to JSP and XML Together | |||||||||||||||||
The Relationship Between XML and JSP | |||||||||||||||||
Java XML/XSL APIs | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 5. Using DOM | |||||||||||||||||
What Is the DOM? | |||||||||||||||||
Nodes and Tree Structure | |||||||||||||||||
Programming with DOM | |||||||||||||||||
JDOM, dom4j, and Deferred DOM | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 6. Programming SAX | |||||||||||||||||
What Is SAX? | |||||||||||||||||
The Workings of SAX | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 7. Successfully Using JSP and XML in an Application | |||||||||||||||||
Using a Java Representation of an XML Document | |||||||||||||||||
Why Not Just Use SAX or DOM? | |||||||||||||||||
Installing JDOM and dom4j | |||||||||||||||||
Why Both JDOM and dom4j? | |||||||||||||||||
Common Ways to Use XML | |||||||||||||||||
Using a Database with XML | |||||||||||||||||
Pulling in XML Files | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 8. Integrating JSP and Web Services | |||||||||||||||||
Thinking in JSP and Web Services | |||||||||||||||||
Integrating a Web Service into a JSP Page | |||||||||||||||||
When Should You Build Your Own Web Service? | |||||||||||||||||
Building a Corporate Web Service | |||||||||||||||||
Apache SOAP Help | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 9. Advanced JSP and XML Techniques | |||||||||||||||||
Accessing Web Services from a Browser | |||||||||||||||||
Handling Large XML Documents | |||||||||||||||||
Handling Special Characters and Encoding | |||||||||||||||||
Using XML Tag Libraries | |||||||||||||||||
Summary | |||||||||||||||||
Part III. Building JSP Sites to Use XML | |||||||||||||||||
Chapter 10. Using XSL/JSP in Web Site Design | |||||||||||||||||
Handling XML Files Directly | |||||||||||||||||
Building an XML Servlet Handler | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 11. Using XML in Reporting Systems | |||||||||||||||||
Architecture of Reporting Systems | |||||||||||||||||
When to Use XML with Reports | |||||||||||||||||
Data Source for Reports | |||||||||||||||||
ResultSet to XML | |||||||||||||||||
Bringing It All Together | |||||||||||||||||
The Sorting Table Stylesheet | |||||||||||||||||
The Cross Tab Stylesheet | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 12. Advanced XML in Reporting Systems | |||||||||||||||||
Multiple-Page Reports | |||||||||||||||||
Reports on Data with One-to-Many Relationships | |||||||||||||||||
Real-World Reporting Systems | |||||||||||||||||
Well-Formed Documents Revisited | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 13. Browser Considerations with XML | |||||||||||||||||
Client-Side XML and Browser Support | |||||||||||||||||
Client-Side JavaScript and XML | |||||||||||||||||
Client-Side Transformations and XML | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 14. Building a Web Service | |||||||||||||||||
Designing a Web Service | |||||||||||||||||
Building the Web Service | |||||||||||||||||
Creating a WSDL File | |||||||||||||||||
Registering Within UDDI | |||||||||||||||||
Using Java to Access a WSDL Document | |||||||||||||||||
Summary | |||||||||||||||||
Chapter 15. Advanced Application Design | |||||||||||||||||
Dynamic JSP | |||||||||||||||||
SOAP Server Security Concerns | |||||||||||||||||
Quick Takes | |||||||||||||||||
Summary | |||||||||||||||||
Part IV. Appendixes | |||||||||||||||||
Appendix A. Setting Up | |||||||||||||||||
Installing the JSP Environment | |||||||||||||||||
NetBeans | |||||||||||||||||
The MySQL Database Server | |||||||||||||||||
Summary | |||||||||||||||||
Appendix B. Introduction to JSP and How Things Work | |||||||||||||||||
JSP Basics | |||||||||||||||||
JSP Actions, Directives, and Implicit Objects | |||||||||||||||||
A More Robust JSP Example | |||||||||||||||||
Additional Information About JSP | |||||||||||||||||
Summary | |||||||||||||||||
Appendix C. Tag Library | |||||||||||||||||
Tag Library Overview | |||||||||||||||||
What Is a Tag Library? | |||||||||||||||||
The Six Steps to Building Tag Libraries | |||||||||||||||||
Tag Library Concepts | |||||||||||||||||
Building a Tag Library | |||||||||||||||||
General Notes | |||||||||||||||||
Summary | |||||||||||||||||
Appendix D. XSL Reference | |||||||||||||||||
XSLT and XPath | |||||||||||||||||
Reference | |||||||||||||||||
Index |
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Addison-Wesley were aware of a trademark claim, the designations have been print ed in initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers discounts on this book when ordered in quantity for special sales.
For more information, please contact:
Pearson Education Corporate Sales Division
201 W. 103rd Street
Indianapolis, IN 46290
(800) 428-5331
corpsales@pearsoned.com
Visit AW on the Web: www.awl.com/cseng/
Copyright 2002 by Pearson Education
All rights reserved. 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, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada.
05 04 03 02 4 3 2 1
First Printing: March 2002
Executive Editor
Rochelle J. Kronzek
Acquisitions Editor
Michelle Newcomb
Development Editor
Mark Ray
Managing Editor
Matt Purcell
Project Editor
Christina Smith
Production Editors
Seth Kerney
Rhonda Tinch-Mize
Matt Wynalda
Indexer
Mandie Frank
Proofreader
Suzanne Thomas
Technical Editor
Craig Pfeifer
Team Coordinator
Pamalee Nelson
Interior Designer
Gary Adair
Cover Designer
Alan Clements
Page Layout
Rebecca Harmon
Michelle Mitchell
Writing this book was a long process. As in all choices, this project had its rewards and its consequences. In the end, the most important lesson I learned was one of life, not JSP. I can share my thoughts in this poem. I dedicate the book to anyone who takes a moment to read my poetry.
Days of Wonder Heaven and Hell are not after life Heaven and Hell are part of life It's in life we make our joy It's in life we build our walls To anyone who reads this may you find your own heaven may you find your own poems Break free of your chains Jump your walls Live life as if in heaven and don't make life hell We have only one life to relive over again in infinity Don't regret as you move forward Since the past is only for memories The secret to life is simply to be true to yourself and smile then you will be able to look upon each new day with the wonder it deserves It's all anyone can ever do Casey Kochmer
I dedicate this book to my family: Jim, Judy, Kristina, and my friends. You have made me what I am today, and for that I thank you.
Life is not measured by the number of breaths we take, but by the moments that take our breath away.
Erica Frandsen
Casey Kochmer is a cofounder of the JSP Insider Web site. As president of AmberJack Software, Casey is a JavaServer Pages (JSP), HTML, XHTML, XML, DHTML, and Web service specialist. He was previously a senior programmer, trainer, and Internet expert for Starling Consulting, where he mentored users in JavaServer Pages, Active Server Pages (ASP), XML usage, and Internet practices. His job was to help customers integrate the latest technology at an appropriate time relative to the needs of a project. His mantra is that a project should implement usable and maintainable technology rather than using the latest technology for technology's sake. Casey codes and builds specialized applications using JSP, ASP, and various databases depending on customer needs. He has coauthored several books, including Professional JSP, Second Edition and Beginning JSP Web Development.
Erica Frandsen is an XML, ASP, and SQL Server expert. Experienced in TCP/IP networking, she is a founder of Sound Home Networks. While working as a network consultant, she installs, configures, and maintains networks for clients. Previously, Erica worked as a programmer and consultant for Starling Consulting, where she designed ASP and SQL systems using XML. Earlier in her career, she used these same technologies to create an online tax paying system for businesses in the state of Washington. She also was the Webmaster for several sites. In her free time, Erica brews beer, plays with computer hardware, and creates Web sites for worthy causes.
I would like to thank Michelle, Mark, and Matt for their excellent work. Without their perseverance this book would never have happened. Thank you, guys!
Also, I pay tribute to Mari Nowitz and Nathan Hamilton. Without my most excellent neighbors, I might have died of starvation and lack of influence from the real world. Thanks for being there when I needed you!
I would also like to thank Steve and Brad at Kundalini for their excellent coffee.
Erica Frandsen
The purpose of this book is to teach you how to implement XML and Web services within a JSP Web application or site. The book will start very simply and then work its way up in complexity. This will make the book accessible to a wide range of readers.
The target audience includes new and intermediate JSP programmers. However, this book will be useful for any JSP programmer who wants to expand his or her XML or Web service implementation knowledge. The book is also geared towards helping a JSP programmer think in terms of the combination of JSP, XML, and Web services. The goal is to show how to usefully integrate these technologies into your projects and share the lessons we have learned in building Web applications.
We are programmers who spend quite a bit of time building Web applications. Over the past few years, we have been implementing XML in our projects. However, implementing XML is easier said than done at times. Even worse, many times XML is implemented in ways that can be harmful to a project. You should never use XML for XML's sake. This book is a reflection of our ordeals in learning the various tools and the methods of incorporating XML in a useful way into a Web site.
The problem for developers hasn't been about finding information on XML, but about using XML successfully within Web applications. While there are plenty of solid XML titles, no title really focuses on how to integrate XML into your JSP project. This book is written with the JSP developer in mind. We want to help teach XML, XSL, XPath, and the entire alphabet soup that goes along with XML. By showing how to use XML within a JSP framework, we intend to help make implementing XML both easy and advantageous for the JSP developer.
Web services are the latest fad. They are so new that many of the accompanying tools are still in beta or are only now being released into the marketplace. It's still very early to learn Web services. Web services are too new for anyone to truly be an expert in the field. This makes learning Web services both an exciting and a strange experience. Our intention is to teach you how to incorporate Web services into JSP. We will remove the confusion that surrounds Web services and give a clear path to learning the basics. This book will show the various elements that constitute a Web service.
The book is divided into four parts. Part I is designed to introduce you to each of the technologies exemplified throughout this book. Part II drills deeply into the various tools for each of these technologies. Part III shows how to successfully combine all these technologies to make your project easier and faster to implement. Part IV contains appendixes that provide reference material.
This part is intended to ground you. We do not assume that you already know a great deal about JSP, XML, or Web services; these three topics are introduced in Chapters 1 3.
This chapter shows how to use JSP and a database together. The chapter serves to ground you in JSP and show you how to perform basic database connectivity.
This chapter is a whirlwind tour of XML and XSL. It introduces each of the major concepts that are needed for XML and begins teaching XSL and XPath.
Chapter 3 introduces the concepts of Web services. Web services are a confusing topic, and the chapter focuses on the basic concepts you will need to use them.
Part II is a review of the tools, APIs, and logic required to successfully implement XML and Web services. These chapters introduce the various concepts of Web services and the various parsers for XML. Once you've studied these chapters, you will have enough knowledge to begin using XML and Web services successfully.
This chapter gives you a quick start to mixing JSP and XML together. The chapter reviews the basic XML APIs and works through some examples of merging JSP and XML.
This chapter teaches the important aspects of the DOM API. The DOM is the standard supported by the W3C for working with an XML file programmatically.
This chapter teaches you the ins and outs of the SAX API. SAX is probably the most common API used to read in an XML file. Most of the time, SAX is used automatically by other XML APIs. However, this chapter is very important because understanding SAX is critical for handling more complicated XML-based processes.
This chapter introduces the other major Java XML APIs: JDOM and dom4j. This chapter produces an integrated example showing how to work with JSP, XML, and a database. The goal of this chapter is to begin walking you through the integration of XML in a natural way within your JSP application.
Chapter 8 examines how to use a Web service within your JSP site. This chapter covers two important Web services topics. The first is using a Web service; the chapter shows the most efficient way to use a Web service within your JSP application. The second topic is building a Web service; the chapter walks through the creation of a Web service that can be used by other applications.
This chapter explores XML concepts that aren't discussed in other chapters. Topics include accessing a Web service from an HTML page, XML encoding issues, ways of processing large XML documents, and XML tag libraries.
Building a JSP site requires far more than just knowing how to use JSP. It requires the ability to think in terms of building a Web application. Web application design is a fine art that involves integrating many different tools as a seamless unit within a JSP project. To this end, Part III covers the implementation of XML and Web services from an application point of view. The chapters in this part cover many topics, from Web service security to building XML reporting systems. We show many different facets of Web application design, from the server to the often-overlooked browser client.
This chapter examines what is possible by going back to the roots of JSP or more specifically by using servlets. The goal of the chapter is to examine generic XML handling. This means that the initial processing of an XML document can happen at the application level rather than at the page level. The chapter shows how to capture the processing of an XML page and route it directly to a servlet.
No matter what the Web application, it's a safe bet that there will be some reporting involved. This chapter examines how reporting systems can benefit from the appropriate placement and use of XML.
This chapter builds on the examples in Chapter 11. Additional concepts and topics to enhance an XML-based reporting system are shown in this chapter's examples. Among the examples is one that shows how to create reports that show a one-to-many relationship in the database.
JSP developers often overlook client-side XML processing. This is a serious oversight, as browsers are a growing and improving XML client-side tool. This chapter examines how using the browser can enhance handling of XML data and reduce Web server load.
In this chapter, we build a Web service system for news delivery. The code within this chapter is based on a Web service within a production environment. The goal is to show the design and full integration of a complete Web service.
This chapter covers two topics. First, we'll show you how to build JSP pages that update themselves. This advanced capability permits JSP sites to be more flexible and to expand what is possible with XML, Web services, and JSP. The second main topic is security; the chapter examines how to secure your Web services.
The appendixes support the material presented in the main part of the book. Their contents are briefly described here.
This appendix covers basic information about setting up the JSP container and introduces NetBeans for the creation of all your JSP pages.
For new JSP users, this appendix offers a crash course on JSP and how it works.
This book uses JSP tag libraries as much as possible. For users who are new to JSP, this appendix quickly covers how to build and use a JSP tag library.
XSL is used extensively in this book. Appendix D is a reference to the most commonly used XSL tags and XPath functions.
All the source code from the listings and programs included in this book is available via download from the Sams Publishing Web site at www.samspublishing.com.
Throughout the book, we create sample class files and JSPs and then build on them later in the same or another chapter. Whenever we do so, we clearly indicate where the original file can be found. Lines of code that should be added or changed are indicated in boldface type.
As we mentioned earlier, this book is geared toward new and intermediate JSP programmers. How you use the book, and where you begin reading, will depend on your experience level. Here are a few tips to get you started.
If possible, we recommend coding the examples. In programming, the best way to learn is by coding, and the examples have been geared to enable you to do so. If you have any questions about initially setting up the JSP container or NetBeans to create JSPs, refer to Appendix A.Otherwise, the first time a specific component is used, it will be referenced for installation at that time.
In many respects, coding the examples is very important because this book explores many different concepts. If you don't write the code, many of the concepts are likely to slip by or not sink in as deeply as they should. We also encourage you to expand and tweak the examples. Try to break the code and find out why it breaks. As programmers, we learn best by coding and by fixing broken code. Coding is best learned by experience; don't shy away from this reality.
We are not going to assume that you already know JSP like the back of your hand. Someone with little or no JSP experience can pick up this book and learn how to use JSP and XML. The code and topics are built in a logical and easy manner to help show what is required in using XML within your JSP projects. Beginning JSP programmers will want to study the chapters in Part I closely. The examples are simple enough that they will be a great learning aid. New JSP readers will also benefit from reading Appendix B.
The best starting place will depend on your skills. For users who are inexperienced in XML or Web services, Part I is still your best bet. Otherwise, we recommend skimming the first section, as there is quite a bit of information within each chapter. These chapters have some information that can benefit more experienced readers. However, if you find that you already know the Part I material, skip ahead to Part II.
The following typographic conventions are used in this book:
Code lines, commands, statements, variables, and any text you type or see onscreen appears in a mono typeface.
Placeholders in syntax descriptions appear in an italic mono typeface. Replace the placeholder with the actual filename, parameter, or whatever element it represents.
Italics highlight technical terms when they're being introduced and defined.
The book also contains Notes to help you spot important or useful information more quickly.
This book was written as a reference for JSP, XML, and Web services. Learning this material on our own was a long process, and our goal is to help give JSP developers some insights into building JSP Web applications. The fact is, XML and Web services are both fast becoming essential tools to most JSP applications. The problem is trying to learn everything at once. Our goal was to provide integrated examples of practical JSP, XML, and Web service implementations. We hope that you benefit from reading the book as much as we benefited from writing it.