Recipe 27.8 Using a JSP to Connect with Amazon


Problem

You want to connect with AWS using a JSP.

Solution

Use the jsp:useBean standard action to create an instance of the AmazonBean from Recipe 27.6. Use this instance to manage the AWS search and results.

Discussion

This recipe uses the same strategy as the JSP in Recipe 27.4: create a JavaBean instance that handles the AWS search and displays the search results. The jsp:useBean standard action creates an instance of com.jspservletcookbook.AmazonBean , which is located in WEB-INF/classes .

Then the code uses jsp:setProperty to set some search options, before the JSP uses jsp:getProperty to launch the seach. Example 27-7 uses the JSTL tag c:catch to catch any exceptions thrown by the AmazonBean 's getSearchResults( ) method. The variable except is of the type Throwable , and its error message is displayed by the c:out tag if the search query is invalid. The JSP in Example 27-7 displays the HTML form and search results shown in Figures Figure 27-3 and Figure 27-4.

This jsp:getProperty code is the equivalent of calling the AmazonBean's getSearchResults( ) method, which returns a String of formatted search results.

Example 27-7. A JSP launches an AWS book search
  <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>  <html> <head><title>Search Amazon.com for a Book</title></head> <body>  <c:choose>    <c:when test="${empty param.keyword}">        <h2>Please enter your Amazon search terms...</h2>      <%-- Display the HTML form... --%>    <form method="POST" action =    '<c:out value="${pageContext.request.contextPath}" />/amazon.jsp'>  <%-- form and table tags... --%>    <table border="0"><tr><td valign="top">        Search terms: </td>  <td valign="top">        <input type="text" name="keyword" size="15">        </td></tr><tr><td valign="top">             <tr><td valign="top">        <input type="submit" value="Submit Info"></td></tr>        </table></form>        </body></html>             </c:when>     <c:otherwise>  <jsp:useBean id="aBean" class="com.jspservletcookbook.AmazonBean" />     <jsp:setProperty name="aBean" property="keyword" param="keyword"/>     <jsp:setProperty name="aBean" property="mode" value="books"/>     <jsp:setProperty name="aBean" property="page" value="1"/>     <jsp:setProperty name="aBean" property="type" value="lite"/>     <jsp:setProperty name="aBean" property="lineSep" value="<br />"/>  <h2>Here are your search results</h2>  <c:catch var="excep">       <%-- Now display any results of the search --%>       <jsp:getProperty name="aBean" property="searchResults" />                          </c:catch >              <%-- Print any error messages, such as 'Bad Request' if the search      terms are meaningless --%>         <c:out value="${excep.message}"/> </c:choose>  </body> </html> 

See Also

The AWS SDK: http://www.amazon.com/gp/aws/download_sdk.html/002-2688331-0628046; Web Services Essentials (O'Reilly); Recipe 27.7 on using a servlet and a JavaBean to connect with AWS; Chapter 23 on using the JSTL.



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

Similar book on Amazon

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