Recipe 27.7 Using a Servlet to Connect with Amazon


Problem

You want to connect with AWS using a servlet.

Solution

Use a specially designed JavaBean to peform the AWS- related tasks .

Discussion

Example 27-6 uses the same design that the Google recipes used, so you should find this servlet code very familiar if you have worked through those examples before. The servlet generates an HTML form in response to a GET HTTP request, which sends the Amazon search terms back to the same servlet. The interesting action takes place in the doPost( ) method, where the servlet uses an AmazonBean class (from Recipe 27.6) to connect with AWS and display any search results.

Example 27-6. A servlet uses a JavaBean to connect with AWS
 package com.jspservletcookbook;     import java.io.IOException;   import java.io.PrintWriter;  import javax.servlet.*; import javax.servlet.http.*; public class AmazonServlet extends HttpServlet {        public void doGet(HttpServletRequest request,      HttpServletResponse response)     throws ServletException, java.io.IOException {            //set the MIME type of the response, "text/html"       response.setContentType("text/html");            java.io.PrintWriter out = response.getWriter( );            //Begin assembling the HTML content       out.println("<html><head>");            out.println(       "<title>Initiate an Amazon Book Search</title></head><body>");       out.println("<h2>Please enter your Amazon search terms</h2>");  //Display an HTML form that sends the request back to this       //'/amazonservlet' which will cause the calling of doPost( )  //make sure method="POST" so that the servlet service method       //calls doPost in the response to this form submit       out.println(         "<form method=\"POST\" action =\"" + request.getContextPath( ) +             "/amazonservlet\" >");       out.println("<table border=\"0\"><tr><td valign=\"top\">");       out.println("Search terms: </td>  <td valign=\"top\">");       out.println("<input type=\"text\" name=\"query\" size=\"15\">");       out.println("</td></tr>");       out.println("<tr><td valign=\"top\">");       out.println(       "<input type=\"submit\" value=\"Submit Info\"></td></tr>");       out.println("</table></form>");       out.println("</body></html>");   } //doGet         public void doPost(HttpServletRequest request,      HttpServletResponse response)       throws ServletException,java.io.IOException{               String query = request.getParameter("query");                boolean isValid = (query == null  query.length( ) < 1) ?       false : true;       response.setContentType("text/html");            java.io.PrintWriter out = response.getWriter( );            out.println("<html><head>");       out.println("<title>Amazon book results</title></head><body>");  if (! isValid){           out.println(           "<h2>Sorry, the query parameter was either empty or null</h2>");       } else {           AmazonBean amBean = new AmazonBean( );           amBean.setKeyword(query);           amBean.setType("lite");           amBean.setMode("books");           amBean.setPage("1");           amBean.setLineSep("<br />");           out.println("<h2>Here are your search results</h2>");           try {               out.println( amBean.getSearchResults( ) );           } catch (Exception e){             out.println(             "The search terms either returned zero results "+             "or were invalid.");         }  }                out.println("</body></html>");        }//doPost }//AmazonServlet 

To keep the code simple, limit the keyword search to books. AWS offers a comprehensive method of searching its several catalogs, however, with the API not limited to keyword searches of books. For example, the product modes include DVD, electronics, music, hardware, software, and toys. You can also initiate several different search types (in addition to keywords), such as Amazon Standard Item Number (ASIN) searches.

Figure 27-3 shows the return value of the servlet's doGet( ) method.

Figure 27-3. A servlet's HTML form accepts Amazon search terms
figs/jsjc_2703.gif

Figure 27-4 shows parts of the servlet's displayed results that are handled by the servlet's doPost( ) method.

Figure 27-4. The results of an Amazon book search
figs/jsjc_2704.gif

Once you have downloaded and unpacked the AWS SDK, the software documentation is located at: AmazonWebServices/API Guide/index.html .


To help debug your AWS searches and servlets, you can initiate an AWS search using a Uniform Resource Locator (URL) in your browser. The following URL initiates a keyword seach for a book using the terms "British Empire."

 http://xml.amazon.com/onca/xml?v=1.0&t=webservices-20&   dev-t=DCJEAVXSDVPUD&KeywordSearch=British%20Empire&mode=books&     type=lite&page=1&f=xml 

An request for this URL returns an XML file that looks similar to Example 27-1. The sales rank reads "N/A" because the search option type=lite returns a null value for this ranking. Use type=heavy to get a value for the Amazon sales rank.

See Also

The AWS SDK http://www.amazon.com/gp/aws/download_sdk.html/002-2688331-0628046; Web Services Essentials (O'Reilly); Recipe 3.1 on mapping a servlet to a name in web.xml ; Recipe 27.8 on using a JSP and a JavaBean to connect with AWS.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net