Creating a JavaBean to Connect with Google API

By: Bruce W. Perry Emailed: 1674 times Printed: 2157 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The first thing to do is get set up with a Google Web Services account.  Now create a JavaBean that will make keyword searches of Google and return the results.

Example java bean below, 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 the below sample program can use it.

A JavaBean that searches Google's web database
  

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 the program above 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.

You can set the various options for Google searches by calling the GoogleSearch setter methods. For example, calling setFilter(true) filters out all the results that derive from the same web host. And you can restrict the search to specific Google subsites by calling setRestrict("mac"). See http://www.google.com/apis/reference.html.

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:

  • getURL( ) returns the URL of the found item

  • getTitle( ) returns the title of the found HTML page

  • getSnippet( ) returns a snippet (a small, possibly ambiguous, piece of text from the web page)

  • getSummary( ) returns a text summary of the found web page

The bean uses these methods to display the URL, title, snippet, and summary of each web page the search returns.


Java Beans Home | All Java Beans Tutorials | Latest Java Beans Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

In this examples please create the output of the programme

View Tutorial          By: bheemesh at 2011-05-01 09:05:46

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Bruce W. Perry
Embedding MP3 audio file in a JSP
Embedding a QuickTime movie in a JSP
Getting HTTP Request Headers in a JSP
Using a DataSource from WebLogic in a JSP
Creating a JavaBean to Connect with Google API
Deploying an Individual JSP on Tomcat
Writing your first JSP page

More Tutorials in Java Beans
Creating a JavaBean to Connect with Google API
Spring Vs EJB ( A feature comparison)
What is EJB server and what are EJB Components?
JavaBeans Basic Concepts
JavaBeans vs. Custom Tags
Java Beans and the Expression Language
A sample that shows Java Beans, Servlets and JSP working together
Advantages of Java Beans
Design Patterns for Properties in a Java Bean
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
ADVANTAGES OF EJB
Steps to develop EJB Environment
EJB is a server side component:
Entity Bean
History Of Java

More Latest News
Most Viewed Articles (in Java Beans )
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
A sample that shows Java Beans, Servlets and JSP working together
Advantages of Java Beans
JavaBeans Basic Concepts
Reusable component
ADVANTAGES OF EJB
Java Beans and the Expression Language
What is EJB server and what are EJB Components?
Spring Vs EJB ( A feature comparison)
Design Patterns for Properties in a Java Bean
JavaBeans vs. Custom Tags
Steps to develop EJB Environment
Entity Bean
Creating a JavaBean to Connect with Google API
EJB is a server side component:
Most Emailed Articles (in Java Beans)
Reusable component
History Of Java
EJB is a server side component:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Steps to develop EJB Environment
Entity Bean
Advantages of Java Beans
Java Beans and the Expression Language
JavaBeans vs. Custom Tags
ADVANTAGES OF EJB
Design Patterns for Properties in a Java Bean
What is EJB server and what are EJB Components?
Spring Vs EJB ( A feature comparison)
A sample that shows Java Beans, Servlets and JSP working together
JavaBeans Basic Concepts