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.
In JSP™ and XML you will:
I. DATA, XML, AND WEB SERVICES INTRODUCTION.1. Integrating JSP and Data.
Using JSP with a Database.
Entering the Data.
Reviewing the Code for Entering Data.
Viewing the Data.
Testing for Scale.
Basic Design Concepts.
Using a Tag library.
Summary.2. Introduction to XML/XSL.
What Is XML?
Rules of XML.
Tags and Elements.
The XML Declaration.
Document Type Declaration.
Well-Formed and Validated Documents.
On to Using XML.
Whitespace and Encoding.
Trees, Nodes, and Family.
Summary.3. Understanding Web Services.
What Is a Web Service?
Crystal Ball Readings.
The ABCs of Web Services.
The Basic Building Blocks.
Service Management Initiatives.
How to Use a Web Service.
Roaming the Internet.
II. INTEGRATING JSP AND XML.4. A Quick Start to JSP and XML Together.
The Relationship Between XML and JSP.
JAXP, Xerces, and Xalan.
JSP and XML: An Overview.
Java XML/XSL APIs.
DOM (XML Document Object Model).
SAX (XML Parser).
JDOM (XML Document Representation).
dom4j (XML Document Representation).
JAXB (Parser and XML Document Representation).
Summary.5. Using DOM.
What Is the DOM?
Strengths of DOM.
Weaknesses of DOM.
Nodes and Tree Structure.
The Document Node.
Programming with DOM.
Removing a Node.
Copying and Appending Nodes.
Programmatically Creating an XML Document.
Moving Nodes Between Documents.
JDOM, dom4j, and Deferred DOM.
Summary.6. Programming SAX.
What Is SAX?
The Workings of SAX.
Downsides to SAX.
Differences Between SAX1 and SAX2.
First SAX Example.
Characters and Ignorable Whitespace.
Processing Versus Validation.
The Document Locator.
Breaking the System to See How It Works.
Processing Versus Validation Revisited.
Using SAX to Output HTML.
Summary.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?
JDOM and dom4j: A Quick Comparison.
Common Ways to Use XML.
Using a Database with XML.
XML Initialization Files.
Storing the Initialization Data.
Using a Listener to Store the DatabaseParameter Object.
Using a Java XML Model.
Getting the Row Count.
XML and the WebRowSet.
Building a dom4j Helper Class.
Creating a Banner Handler.
Creating a Test JSP Page.
Using a Java Representation of an XML Document.
HashMap Versus Java XML Representation.
Pulling in XML Files.
Defining an XML File.
Pondering XML Design.
Reading XML Files and Creating New Output.
Building the Final JSP Page.
Summary.8. Integrating JSP and Web Services.
Thinking in JSP and Web Services.
Tag Libraries Versus Web Services.
Using Tag Libraries to Access a Web Service.
Integrating a Web Service into a JSP Page.
A Tag Library/Service Warning.
Fixing Some Network Issues.
Web Service Reliability.
When Should You Build Your Own Web Service?
JSP Pages Versus Web Service.
Building a Corporate Web Service.
Goal of This Web Service Example.
Realities of Building a Web Service.
Setting Up the Example.
Accessing Application Data.
Building the Actual Web Service.
Deploying a Web Service.
Where Is the WSDL?
Writing a JSP Page to Deploy the Descriptor File.
More on Security.
Building a Page to Access the Service.
Apache SOAP Help.
Summary.9. Advanced JSP and XML Techniques.
Accessing Web Services from a Browser.
Using an Applet.
Handling Large XML Documents.
Handling Special Characters and Encoding.
Using XML Tag Libraries.
XSL Tag Library.
XTags Library for XML.
III. BUILDING JSP SITES TO USE XML.10. Using XSL/JSP in Web Site Design.
Handling XML Files Directly.
How Servlet Mappings Work.
Building an XML Servlet Handler.
Building a SAX Reader.
Creating a Servlet to Process XML Files.
Register the Servlet.
Building the Error Page.
Creating Some Test Files.
Accessing XML Directly.
Summary.11. Using XML in Reporting Systems.
Architecture of Reporting Systems.
When to Use XML with Reports.
Data Source for Reports.
Creating Database Data.
ResultSet to XML.
What It Does.
Bringing It All Together.
The Sorting Table Stylesheet.
The Cross Tab Stylesheet.
Summary.12. Advanced XML in Reporting Systems.
The JSP for the Multiple-Page Report.
The Stylesheet for the Multiple-Page Report.
Reports on Data with One-to-Many Relationships.
The JSP for the One-to-Many Report.
The Stylesheet for the One-to-Many Report.
Real-World Reporting Systems.
Well-Formed Documents Revisited.
Summary.13. Browser Considerations with XML.
Client-Side XML and Browser Support.
Client-Side Transformations and XML.
The Two XSL Stylesheets.
Summary.14. Building a Web Service.
Designing a Web Service.
What Is the Goal?
What Are the Requirements?
What Data Does the Service Need?
Building the Web Service.
Building a File Handler.
Building a Search Utility.
Creating an ElementHandler.
Building a Document Object.
Applying a Stylesheet.
Creating a Stylesheet.
Building the Web Service at Last.
Registering the Web Service with Apache SOAP.
Creating a WSDL File.
Creating the JSPBuzz WSDL File.
WSDL Implementation File.
Registering Within UDDI.
Registering a Service.
Using Java to Access a WSDL Document.
Summary.15. Advanced Application Design.
When Not to Use Dynamic JSP.
Building a Dynamic JSP Example.
SOAP Server Security Concerns.
Using Tomcat Security Zones.
Other Apache SOAP-Specific Security Steps.
Web Services—SSL and Data Encryption.
IV. APPENDIXES.A. Setting Up.
Installing the JSP Environment.
The Java Software Development Kit (SDK).
The Tomcat Server.
Creating a Web Site for the Book.
The MySQL Database Server.
Creating a MySQL Database.
Installing a JDBC Driver.
Summary.B. Introduction to JSP and How Things Work.
JSP Banner Example.
JSP Actions, Directives, and Implicit Objects.
A More Robust JSP Example.
Additional Information About JSP.
What Is JSP and How Does It Work?
JSP XML Syntax.
JSP Documentation Resources.
Summary.C. Tag Library.
Tag Library Overview.
What Is a Tag Library?
The Six Steps to Building Tag Libraries.
Tag Library Concepts.
Isolating the Business Logic.
The Tag Handler.
The Tag Library Descriptor (TLD).
Creating a Distribution File.
Registering the Tag Library.
Using the Tag Library Declaration on a JSP Page.
Building a Tag Library.
Isolating the Business Logic.
Building a Tag Handler.
The Tag Library Descriptor.
Registering the Tag Library.
Using the Tag Library on a JSP Page.
Summary.D. XSL Reference.
XSLT and XPath.
Context and Current Nodes.
XPath Functions.Index. 0672323540T03282002
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.
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:
italic monotypeface. Replace the placeholder with the actual filename, parameter, or whatever element it represents.
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.