[ Team LiB ] |
![]() ![]() |
Recipe 27.2 Creating a JavaBean to Connect with GoogleProblemYou want to use Google's Web APIs to make Java-enabled searches to Google's site. SolutionCreate a JavaBean so that you can use the bean in both a servlet and JSP. DiscussionThe first thing to do is get set up with a Google Web Services account, as described in Recipe 27.1. Now create a JavaBean that will make keyword searches of Google and return the results. Example 27-2 first imports the package contained in googleapi.jar: com.google.soap.search. Remember, you stored that JAR file in WEB-INF/lib. This means that the web application can find the Java classes in that package and the GoogleBean in Example 27-2 can use it. Example 27-2. A JavaBean that searches Google's web databasepackage com.jspservletcookbook; import com.google.soap.search.*; public class GoogleBean { private GoogleSearch search; private GoogleSearchResult googleRes; private final static String GOOGLE_KEY = "5W1BWPyzPSyI3rIa5Pt3DtXMatsniSGB"; private String lineSep = "\n"; //Settable bean properties private String query; private boolean filter; private int maxResults; private int startRes; private boolean safeSearch; private String restrict; private String langRestrict; public GoogleBean( ){ //No-arguments constructor for the bean query = ""; restrict = ""; langRestrict = ""; } public String structureResult(GoogleSearchResult res){ //Each GoogleSearchResultElement GoogleSearchResultElement[] elements = res.getResultElements( ); String url =""; String results = "Estimated total results count: " + res.getEstimatedTotalResultsCount( ) + lineSep + lineSep; for (int i = 0; i < elements.length; i++){ url = elements[i].getURL( ); results += ("Title: " + elements[i].getTitle( ) + lineSep + "URL: <a href=\"" + url + "\">" + url + "</a>"+ lineSep + "Summary: " + elements[i].getSummary( ) + lineSep + "Snippet: " + elements[i].getSnippet( ) + lineSep + lineSep); } return results; } public String getSearchResults( ) throws GoogleSearchFault { search = new GoogleSearch( ); search.setKey(GOOGLE_KEY); search.setFilter(filter); if(restrict.length( ) > 0) search.setRestrict(restrict); search.setQueryString(query); googleRes = search.doSearch( ); return structureResult(googleRes); } public void setLineSep(String lineSep){ this.lineSep=lineSep; } public String getLineSep( ){ return lineSep; } public void setQuery(String query){ this.query = query; } public String getQuery( ){ return query; } public void setRestrict(String query){ this.restrict = restrict; } public String getRestrict( ){ return restrict; } public void setLangRestrict(String langRestrict){ this.langRestrict = langRestrict; } public String getLangRestrict( ){ return langRestrict; } public void setFilter(boolean filter){ this.filter = filter; } public boolean getFilter( ){ return filter; } public void setSafeSearch(boolean safeSearch){ this.safeSearch = safeSearch; } public boolean getSafeSearch( ){ return safeSearch; } public void setMaxResults(int maxResults){ this.maxResults = maxResults; } public int getMaxResults( ){ return maxResults; } public void setStartRes(int startRes){ this.startRes = startRes; } public int getStartRes( ){ return startRes; } }//GoogleBean The interesting action in Example 27-2 occurs in the methods getSearchResults( ) and structureResults( ). In getSearchResults( ), the code creates a GoogleSearch object, which is then customized with Google search options before the GoogleSearch doSearch( ) method is called. The GoogleSearch object uses setter methods to design a specific google.com search. For example, the setQueryString( ) method provides the user's search terms. The Java objects that are using the bean provides the search terms by calling the bean's setQuery( ) method.
Every SOAP-related search of Google must call the GoogleSearch setKey( ) method with the proper license key, or the search is rejected. The structureResults( ) method formats the search results. Google search results are encapsulated by a GoogleSearchResult object. This object contains an array of GoogleSearchResultElement objects, which represent each URL that the Google search has returned. The GoogleSearchResult getResultElements( ) method returns the GoogleSearchResultElement array. The code then iterates through the array. Each returned element (the GoogleSearchResultElement object) has getter or accessor methods that provides information about the web-page search result: The bean uses these methods to display the URL, title, snippet, and summary of each web page the search returns. Figure 27-2 shows how these results are displayed. 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. |
[ Team LiB ] |
![]() ![]() |