[ Team LiB ] Previous Section Next Section

Recipe 27.7 Using a Servlet to Connect with Amazon


You want to connect with AWS using a servlet.


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


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"
      java.io.PrintWriter out = response.getWriter( );
      //Begin assembling the HTML content
      "<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
        "<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("<tr><td valign=\"top\">");
      "<input type=\"submit\" value=\"Submit Info\"></td></tr>");

  } //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;

      java.io.PrintWriter out = response.getWriter( );
      out.println("<title>Amazon book results</title></head><body>");
      if (! isValid){
          "<h2>Sorry, the query parameter was either empty or null</h2>");
      } else {
          AmazonBean amBean = new AmazonBean( );
          amBean.setLineSep("<br />");
          out.println("<h2>Here are your search results</h2>");
          try {
              out.println( amBean.getSearchResults( ) );
          } catch (Exception e){
            "The search terms either returned zero results "+
            "or were invalid.");

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

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

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."


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.

    [ Team LiB ] Previous Section Next Section