O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

27.6. Creating a JavaBean to Connect with Amazon

Problem

You want to create a JavaBean as a type of Amazon search utility class.

Solution

Set up your Amazon API as described in Recipe 27.5, then code a JavaBean that uses the com.amazon.soap.axis package from this API.

Discussion

The JavaBean in Example 27-5, named AmazonBean, imports the com.amazon.soap.axis package. This package is stored in amazonapi.jar, which (generated by Recipe 27.5). Store the JAR in the web application's WEB-INF/lib directory and the AmazonBean in WEB-INF/classes (or also in a JAR in WEB-INF/lib).

Example 27-5 connects with Amazon in its getSearchResults( ) method. The AmazonBean formats and displays the search results in structureResults( ) . The code comments describe what's going on in detail.

Example 27-5. A JavaBean class that searches Amazon

package com.jspservletcookbook;import java.net.URL;           

import com.amazon.soap.axis.*;

public class AmazonBean {

    //The developer's token
    private final static String AMAZON_KEY = "DCJEAVDSXVPUD";

    //NOTE: AWS Version 3 uses "http://xml.amazon.com/xml3"
    private final static String END_POINT = 
      "http://soap.amazon.com/onca/soap";

    private final static String AMAZON_TAG = "webservices-20";
  
    private URL endpointUrl;
  
    private String lineSep = "\n";
    private String totalResults;
    private String keyword;
    private String page;
    private String type;
    private String mode;
  

  public AmazonBean( ){}//no-arguments constructor required for a bean //an easy way to test the bean outside of a servlet ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required