Recipe 27.3 Using a Servlet to Connect with Google


Problem

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

Solution

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

Discussion

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 {            response.setContentType("text/html");                java.io.PrintWriter out = response.getWriter( );                out.println("<html><head>");       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       out.println(         "<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\">");           out.println(       "Restrict to Google sub-site... </td>  <td valign=\"top\">");       out.println(         "<select name=\"restrict\"><option>unclesam</option>"+         "<option>linux</option>option>mac</option><option>bsd</option>"+         "</select>");       out.println("</td></tr><tr><td valign=\"top\">");       out.println(       "<input type=\"submit\" value=\"Submit Info\"></td></tr>");       out.println("</table></form>");       out.println("</body></html>");      } //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"       response.setContentType("text/html");         java.io.PrintWriter out = response.getWriter( );       out.println("<html><head>");       out.println("<title>Google results</title></head><body>");  if (! isValid){               out.println(           "<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( );               gb.setFilter(true);               //Configure for web display                gb.setLineSep("<br />");               if (restrict != null && restrict.length( ) > 0)               gb.setRestrict(restrict);               gb.setQuery(query);               try {                   out.println( gb.getSearchResults( ) );               } catch (Exception e){                   throw new ServletException( e.getMessage( ) );               }  }//if           out.println("</body></html>");            }// doPost }//GoogleServlet 

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
figs/jsjc_2701.gif

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
figs/jsjc_2702.gif

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.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net