Google and Amazon.com are both early adopters in the emerging field of web services.
Google is a giant web search engine and directory. Amazon.com is a e-commerce web site that began as an online bookstore and has since branched out into numerous products such as software and electronics. Both sites separately offer software developers web services Application Programming Interfaces (APIs) that give you the ability to manage Google searches using Java objects and access Amazon's comprehensive product catalogs with your Java code.
For us Java developers, web services means making requests and receiving responses using a special XML format. In other words, you make a request using XML elements and attributes in text form, and receive a response in the same format. Web services typically use an XML-based protocol named Simple Object Access Protocol (SOAP) to transfer information.
In a nutshell , SOAP represents the abstraction of an envelope, that in turn contains optional headers and the message body. The message, composed of its outer envelope, as well as the headers and body, is made up of XML elements that are associated with specified namespaces. The technologies this chapter describes use HTTP to carry these XML-based SOAP messages.
I never really understood SOAP messages until I looked at some samples. Example 27-1 is part of a SOAP response to an Amazon Web Services keyword-search request using the query "Lance Armstrong."
The response is an XML file composed of a ProductInfo root element, which contains one or more Details elements. Each one of these Details represents a book from Amazon's catalog (I omitted all but one of the Details elements, just to make the sample easier to view). Only one of the returned books is shown.
Example 27-1. A SOAP response from Amazon Web Services based on a searchfor the terms "Lance Armstrong"
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ProductInfo PUBLIC "-//Amazon.com //DTD Amazon Product Info//EN" "http://xml.amazon.com/schemas/dev-lite.dtd"> <ProductInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "http://xml.amazon.com/schemas/dev-lite.xsd"> <Details url= "http://www.amazon.com/exec/obidos/ASIN/0399146113/webservices-20? dev-t=DCJEAVXSDVPUD%26camp=2025%26link_code=xm2"> <Asin>0399146113</Asin> <ProductName>It's Not About the Bike: My Journey Back to Life </ProductName> <Catalog>Book</Catalog> <Authors> <Author>Lance Armstrong</Author> <Author>Sally Jenkins</Author> </Authors> <ReleaseDate>June, 2000</ReleaseDate> <Manufacturer>Putnam Pub Group</Manufacturer> <ImageUrlSmall> http://images.amazon.com/images/P/0399146113.01.THUMBZZZ.jpg </ImageUrlSmall> <ImageUrlMedium> http://images.amazon.com/images/P/0399146113.01.MZZZZZZZ.jpg </ImageUrlMedium> <ImageUrlLarge> http://images.amazon.com/images/P/0399146113.01.LZZZZZZZ.jpg </ImageUrlLarge> <ListPrice>.95</ListPrice> <OurPrice>.47</OurPrice> <UsedPrice>.99</UsedPrice> </Details> </ProductInfo>
Three principal reasons for adopting SOAP-based web services are:
A recipe introduction cannot do justice to a complicated topic such as SOAP, but there are plenty of books and free tutorials on this topic (see this chapter's "See Also" sections for some suggestions).
Mostly in a beta stage of development, the Amazon and Google web services APIs allow a Java program to create very useful and complex systems that interact with Amazon and Google. The Amazon and Google web services programs are designed to familiarize developers with these new ways of handling requests and responses to the two popular web destinations.
The programs generally involve creating a developer's account and recieving a key, or token, that will accompany each one of your requests to these sites. This chapter describes how to get set up with using Amazon and Google web services, then shows you how to integrate these APIs with a servlet and JSP.