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

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