JSP™ and XML Integrating XML and Web Services in Your JSP™ Application By Casey Kochmer, Erica Frandsen Publisher : Addison Wesley Pub Date : March 19, 2002 ISBN : 0-672-32354-0 Pages : 592 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 Table of businesses will drive system-to-system communication • Contents JSP(TM) and XML takes you beyond the basics, giving you practical advice and in-depth coverage In the book, • Index 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 Copyright 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 Credits 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 Dedications 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 About the Authors 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 trees structures families nodes TreeWalker parameters TreeWalker objects 2nd 3rd 4th triggers start events troubleshooting WSDL documents true Boolean value true() boolean function try blocks DOM exceptions try-catch block try-catch blocks Java databases JSP pages tutorials character encoding DTD (document type definition) Java servlets two-pass process embedded stylesheet declarations types data attributes [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] UDDI (Universal Description Discover, and Integration) accessing accounts creating APIs SOAP business listing (Internet) registering (Web services) 2nd 3rd registries search feature versus ebXML Web service component 2nd Web services registering 2nd 3rd Web site white pages yellow pages unicode XML Uniform Resource Identifier (URI) DTD (document type definition) uniform resource identifiers [See URI] Universal Description, Discovery, and Integration [See UDDI] updating Tomcat user XML documents URI (uniform resource identifier) URI (Uniform Resource Identifier) DTD (document type definition) URIs assigning database tags tag libraries registering URLs tab libraries registering usebean actions JSP element 2nd usernames Tomcat security zones UTF-8 encoding 2nd errors utilities search building building (Web services) [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] validated documents validating elements (namespaces) parsers DTD XML files 2nd validating parsers validation errors fatal errors versus processing SAX versus processing (SAX) validation errors values attribute behavior attributes banners storing (example) 2nd Boolean false square bracket [] statements 2nd true XPath 2nd 3rd contents element declarations string objects variables application JSP containers Internet Explorer Locator creating Netscape setting sniff() function 2nd viewing data databases 2nd 3rd 4th 5th sources XML files browsers views creating XML files [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] W3C (World Wide Web Consortium) DOM DOM (Document Object Model) SOAP WSDL war [period before] files Tomcat warning SAX error event Web application databases (managing) applications Java classes Web services;future of 2nd containers Tomcat server Web service information required use of Web Service Description Language [See WSDL] Web services 2nd 3rd access designing accessing 2nd applets 2nd 3rd 4th 5th 6th 7th browsers 2nd 3rd 4th 5th 6th data (required) 2nd accessing (tag libraries) advantages 2nd accessing code;registered creating locating logic advantages)speed arguments building 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th applying stylesheets building Document objects building file handlers 2nd building search utilities 2nd creating ElementHandler 2nd 3rd 4th creating stylesheets 2nd 3rd JavaBeans building (personal) 2nd 3rd building (registering) calling calls storing choosing 2nd 3rd 4th classes accessing components 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th Apache SOAP (Java API) AXIS (Java API) basics 2nd 3rd 4th 5th 6th 7th 8th ebXML Java APIs 2nd JAXM (Java API) JAXR (Java API) JWSDL (Java API) service management initiatives 2nd 3rd 4th SOAP 2nd 3rd UDDI 2nd WSDL 2nd 3rd 4th 5th XML considerations creating data encryption defined 2nd defining WSDL deploying deployment descriptor XML files describing designing 2nd data goals of requirements 2nd disadvantage programming software layers disadvantages security disadvantages)tools automated dom4j Document object building errors example of featurelocating features 2nd centralized code;reusing distribution Internet protocols files storing firewalls frameworks elements functions future of 2nd automated tools design 2nd implementation Java markets programming tools Web applications 2nd goals how to use 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th Internet (roaming) 2nd 3rd 4th SOAP 2nd 3rd 4th 5th 6th 7th 8th identifying information packets interating JSP pages 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th invoking JSP 2nd 3rd 4th levels message sending messages methods calling operation elements methods (portType element) multiple documents merging networking issues pages linking process calls process of published queries transferring (SOAP registering UDDI 2nd 3rd remote requests responses transferring (SOAP search arrays building search utilities building servers 2nd specifications SSL string packets syntax testing UDDI registering 2nd 3rd versus JSP pages versus Web sites verus tag libraries 2nd WSDL methods Web services (corporate) building accessing 2nd 3rd 4th application data building classes 2nd 3rd deploying 2nd 3rd 4th 5th example (setting up) goal of intializing data 2nd 3rd realities of security writing JSP pages 2nd WSDL Web sites Apache Apache SOAP creating dom4j ebXML IBM 2nd Jakarta JavaRanch JDOM JSPInsider Microsoft MySQL Sun Sun Java UDDI versus Web services XMethods web.xml file WebRowSet class XML databases well-formed documents white pages UDDI whitespace encoding (XSL) ignorable SAX 2nd 3rd 4th 5th nodes World Wide Web Consortium [See W3C] write statements writing clients Web services (accessing) JavaBeans Web services (accessing) 2nd select statements WSDL (Web Service Description Language) document example of 2nd documentation documents accessing (Java) 2nd 3rd 4th binding attributes JWSDL location attributes parameters (messages) ports transport attributes implementation files JSPBuzz documents creating 2nd JSPBuzz files creating 2nd 3rd 4th namespaces declaring network services root elements tools troubleshooting W3C Web service component 2nd 3rd 4th 5th Web services defining methods WSDL documents creating 2nd 3rd 4th 5th 6th 7th 8th 9th [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] Xalan API XML Xerces API XML downloading Java DOM parser Tomcat XMethods Web site XML (Extensible Markup Language) accessing (directly) API JAXP 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th Xalan Xerces APIs 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th character text comments component Web services data managing navigating storage data blocks building (JAXB) databases 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th banner handlers 2nd dom4j helper classes HashMap initialization files 2nd 3rd 4th Java XML model 2nd 3rd 4th JAXB listeners 2nd 3rd row counts test JSP pages 2nd threading WebRowSet class XML documents (Java representations) documents 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th building JSP pages 2nd creating output 2nd 3rd data placement data storage defining 2nd 3rd design 2nd element placement Java representations Java-optimized JDOM parsing (SAX) reading 2nd 3rd reading (JDOM) reading (SAX) ebXML Web component element data accessing 2nd 3rd 4th elements (selecting) templates features files displaying data (syntax) 2nd handling 2nd 3rd 4th handling (customized JSP) 2nd large (handling) 2nd 3rd 4th 5th 6th 7th loading (Internet Explorer) loading (Netscape) processing (servlets) 2nd 3rd 4th 5th transforming (external stylesheets) validating layout logic abstraction layers precautions 2nd 3rd processing 2nd 3rd processors accessing reading files classes (syntax) relating to JSP 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd rules 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th self-describing stylesheets syntax (JSP) tags actions test files creating (servlet handlers) 2nd 3rd 4th 5th 6th text values selecting (tags) transforming unicode XML (and XSL) JAXP example 2nd 3rd example of XML declarations special character encoding XML files large data defining size XPath attributes asterisk (*) 2nd selecting Boolean values 2nd 3rd documents elements asterisks (*) 2nd 3rd expressions square brackets [] 2nd functions 2nd 3rd 4th 5th 6th boolean number 2nd set node 2nd string output creating relative paths 2nd 3rd row count selection statements 2nd 3rd 4th 5th 6th XSL 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th XPath features dom4j XSL 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th (Extensible Stylesheet Language) 2nd entity declarations 2nd families matching namespaces nodes 2nd parameters example of (JAXP) 2nd 3rd 4th 5th processors accessing stylesheet errors stylesheet linking 2nd stylesheets creating output Cross Tab Stylesheet 2nd 3rd 4th 5th Sorting Table Stylesheet 2nd 3rd 4th 5th XML files (viewing) templates 2nd 3rd 4th 5th 6th 7th transforming 2nd tree structures whitespace encoding XPath 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th XSLT XSL (and XML) JAXP example 2nd 3rd example of XSL Formatting Objects [See XSL-FO] XSL tag library 2nd 3rd XSL Transformations [See XSLT] XSL-FO (XSL Formatting Objects) XSLT (XSL Transformations) elements 2nd 3rd 4th nodes 2nd 3rd XSL XTags tag library 2nd 3rd 4th 5th 6th [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] yellow pages UDDI ... 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... 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: Integrating JSP and Web Services Chapter 8 examines how to use a Web service within your JSP site... We hope that you benefit from reading the book as much as we benefited from writing it Part I: Data, XML, and Web Services Introduction IN THIS PART 1 Integrating JSP and Data 2 Introduction to XML/ XSL 3 Understanding Web Services