[ Team LiB ] |
![]() ![]() |
Recipe 27.4 Using a JSP to Connect with GoogleProblemYou want to search Google using the Google Web APIs and a JSP. SolutionUse the jsp:useBean standard action to get access to the GoogleBean from Example 27-2, then use this bean instance to connect with Google's web tools. DiscussionThe JSP in Example 27-4 uses the JSTL core tags to determine if the user has sent a search query along with their request. If the query request parameter is empty, then the JSP displays a form (see Figure 27-1). See Chapter 23 for details on the JSTL core tags. If the request parameter is filled by a search query, the JSP uses the GoogleBean to search google.com and display the results. The JSP uses the jsp:useBean standard action to create an instance of the bean, which is stored in the WEB-INF/lib directory. Example 27-4. A JSP uses a JavaBean to search google.com<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <head><title>Search Google from a JSP</title></head> <body> <c:choose> <c:when test="${empty param.query}"> <h2>Please enter your search terms...</h2> <%-- Display the HTML form... --%> <form method="POST" action ='<c:out value= "${pageContext.request.contextPath}" />/google.jsp'> <table border="0"> <tr><td valign="top"> Search terms: </td> <td valign="top"> <input type="text" name="query" size="15"> </td></tr> <tr><td valign="top"> Restrict to Google sub-site... </td> <td valign="top"> <select name="restrict"> <option selected>none</option><option>unclesam</option> <option>linux</option> <option>mac</option><option>bsd</option></select> </td></tr> <tr><td valign="top"> <input type="submit" value="Submit Info"></td></tr> </table></form> <%-- End of the HTML form... --%> </c:when> <c:otherwise> <%-- Create an instance of the GoogleBean --%> <jsp:useBean id="gBean" class="com.jspservletcookbook.GoogleBean" /> <h2>Here are your search results</h2> <%-- Set the query, restrict, and lineSep properties of the GoogleBean --%> <jsp:setProperty name="gBean" property="query" param="query"/> <jsp:setProperty name="gBean" property="restrict" param="restrict"/> <jsp:setProperty name="gBean" property="lineSep" value="<br /><br />"/> <%-- Now display any results of the search --%> <jsp:getProperty name="gBean" property="searchResults" /> </c:otherwise> </c:choose> </body> </html> The JSP uses the jsp:setProperty standard action to the bean instance's query, restrict, and lineSep properties. The query represents the search terms; restrict can have values of mac, linux, bsd, or unclesam, representing various Google sub-sites, and the lineSep property determines the line-separation characters to use when formatting the results (<br/> in this example). Finally, the code uses jsp:getProperty to effectively call the GoogleBean's getSearchResults( ) method, which sends a SOAP message to Google and formats the response. See AlsoThe home for Google Web Service: http://www.google.com/apis/; the Google web APIs SDK: http://www.google.com/apis/download.html; Recipe 27.1 on setting up your programming environment for use with the Google Web APIs; Recipe 27.3 on using a servlet to connect with Google web services. ![]() |
[ Team LiB ] |
![]() ![]() |