[ Team LiB ] Previous Section Next Section

Recipe 27.3 Using a Servlet to Connect with Google


You want to connect to Google with a servlet and initiate a search.


Use the JavaBean described in Recipe 27.2 as a Google search utility class.


The servlet in Example 27-3 uses the GoogleBean from Recipe 27.2 to initiate google.com searches and display the results.

The servlet displays an HTML form in its doGet( ) method. The client uses this form to input Google search parameters, and then POST the form parameters back to the same servlet. Finally, the servlet's doPost( ) method creates an instance of the GoogleBean to initiate the search. In this case, use the deployment descriptor to map any requests of the form "/googleservlet" to Example 27-3.

Example 27-3. A servlet uses a special JavaBean to search Google and display any results
package com.jspservletcookbook;    

import java.io.IOException;  
import java.io.PrintWriter;     

import javax.servlet.*;
import javax.servlet.http.*;

public class GoogleServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, 
    HttpServletResponse response)
    throws ServletException, java.io.IOException {
      java.io.PrintWriter out = response.getWriter( );
      out.println("<title>Initiate a Google Search</title></head><body>");
      out.println("<h2>Please enter your search terms</h2>");
      //Make sure method="POST" so that the servlet service method
      //calls doPost in the response to this form submit
        "<form method=\"POST\" action =\"" + request.getContextPath( ) +
            "/googleservlet\" >");

      out.println("<table border=\"0\"><tr><td valign=\"top\">");
      out.println("Search terms: </td>  <td valign=\"top\">");
      out.println("<input type=\"text\" name=\"query\" size=\"15\">");
      out.println("</td></tr><tr><td valign=\"top\">");
      "Restrict to Google sub-site... </td>  <td valign=\"top\">");
        "<select name=\"restrict\"><option>unclesam</option>"+
      out.println("</td></tr><tr><td valign=\"top\">");
      "<input type=\"submit\" value=\"Submit Info\"></td></tr>");
     } //doGet
  public void doPost(HttpServletRequest request, 
    HttpServletResponse response)
    throws ServletException,java.io.IOException{
      String query = request.getParameter("query");
      String restrict = request.getParameter("restrict");
      boolean isValid = (query == null || query.length( ) < 1) ?
          false : true;

      //set the MIME type of the response, "text/html"
      java.io.PrintWriter out = response.getWriter( );

      out.println("<title>Google results</title></head><body>");
      if (! isValid){
          "<h2>Sorry, the query parameter was either empty or null</h2>");
          } else {
              out.println("<h2>Here are your search results</h2>");
              GoogleBean gb = new GoogleBean( );
              //Configure for web display 
              gb.setLineSep("<br />");
              if (restrict != null && restrict.length( ) > 0)
              try {
                  out.println( gb.getSearchResults( ) );
              } catch (Exception e){
                  throw new ServletException( e.getMessage( ) );
  }// doPost

Using the GoogleBean class in doPost( ) is straightforward. The code sets a few search options (such as setFilter(true)), then calls the bean's getSearchResults( ) method. This method returns a String of formatted search results, which the servlet's PrintWriter sends to the browser for display.

Figure 27-1 shows the simple HTML form displayed in the servlet's doGet( ) method.

Figure 27-1. Enter keywords to search Google with a servlet

The "Restrict to Google sub-site . . . " part allows the user to choose one of none, unclesam, linux, mac, or bsd. The user enters the search term "Lance Armstrong" in the HTML form's text field, then presses the "Submit Info" button. Figure 27-2 shows the search results dispalyed by the servlet's doPost( ) method.

The Google Web APIs display a maximum of 10 results per search.

Figure 27-2. A servlet using the Google Web APIs displays some search results

See Also

The home for Google Web Service: http://www.google.com/apis/; the Google Web APIs SDK: http://www.google.com/apis/download.html; Recipe 3.1 on mapping a servlet to a name in web.xml; Recipe 27.1 on setting up your programming environment for use with the Google Web APIs; Recipe 27.4 on using a JSP to connect with Google web services.

    [ Team LiB ] Previous Section Next Section