爱心技术专栏专题

中检索Google的搜索结果

摘录:java 来源:java 加入时间:2006年08月19日
摘要:
中检索Google的搜索结果

  
客户端的JavaScript:

<script language="javascript">
var theURL = "/examples/servlet/MyGoogleSearchServlet?search=";
var http = getH…

转载:转载请保留本信息,本文来自
http://www.51dibs.com
/html/2006/article/info16/a_85f19ce80343dadf.htm

中检索Google的搜索结果

站点:爱心种子小博士 关键字:中检索Google的搜索结果

   
中检索Google的搜索结果
  
客户端的JavaScript:

<script language="javascript">
var theURL = "/examples/servlet/MyGoogleSearchServlet?search=";
var http = getHTTPObject(); 

function handleHttpResponse() {
if (http.readyState == 4) {
    GoogleSearchResults.innerHTML = http.responseText;
}
}

function updateGoogleResults(formElement) {
var searchQuery = document.forms[0].elements[formElement].value;
http.open("GET", theURL + escape(searchQuery), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}

function getHTTPObject() {
var xmlhttp;
try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
     try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (E) {
        xmlhttp = false;
     }
}

if (!xmlhttp && typeof XMLHttpRequest != ’undefined’) {
    try {
     xmlhttp = new XMLHttpRequest();
    } catch (e) {
     xmlhttp = false;
    }
}

return xmlhttp;
}
</script>

__________________________________________________________________________

服务器端的:MyGoogleSearchServlet

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Search Google.

* @author Jose Sandoval
*/
public class MyGoogleSearchServlet extends HttpServlet {
        /**
         * Do get.
         * 
         * @param request HttpServletRequest
         * @param response HttpServletResponse
         * @throws ServletException, IOExeption
         */
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                PrintWriter out = response.getWriter();
                StringBuffer resultBuffer = new StringBuffer();
                ArrayList results = MyGoogleSearch.doSearch(request.getParameter("search"));
                SearchResultBean resultBean = null;
                
                for (int i = 0; i<results.size(); i++) {
                        resultBean = (SearchResultBean) results.get(i);
                        resultBuffer.append("<a href=").append(resultBean.getURL()).append(">").append(resultBean.getTitle()).append("</a>");
                        resultBuffer.append("<br>");
                        resultBuffer.append(resultBean.getSnippet());
                        resultBuffer.append("<br>").append("<br>");
                }
                
                response.setContentType("text/html");
                response.setHeader("Pragma", "no-cache");
                response.setHeader("Expires", "0");
                response.setHeader("Cache-Control", "no-store");
                
                out.print(resultBuffer.toString());
                out.close();
        }
}

__________________________________________________________________________

服务器端的:MyGoogleSearch

import java.io.IOException;
import java.util.ArrayList;

import com.google.soap.search.GoogleSearch;
import com.google.soap.search.GoogleSearchFault;
import com.google.soap.search.GoogleSearchResult;
import com.google.soap.search.GoogleSearchResultElement;

/**
* GoogleSearch.

* @author Jose Sandoval
*/
public class MyGoogleSearch {
        private static String CLIENT_KEY = "YOU_HAVE_TO_GET_YOUR_OWN_KEY_FROM_GOOGLE";

        /**
         * Search Google.
         * 
         * @param queryString String
         * @return ArrayList Contains SimpleSearchResult
         * @throws IOException
         */
        public static ArrayList doSearch(String queryString) throws IOException {
                ArrayList searchResults = new ArrayList();
                GoogleSearch search = new GoogleSearch();
                search.setKey(CLIENT_KEY);
                
                try {
                        search.setQueryString(queryString.trim());
                        GoogleSearchResult results = search.doSearch();
                        GoogleSearchResultElement[] resultElements = results.getResultElements();
                        
                        for (int i = 0; i<resultElements.length; i++) {
                                searchResults.add(new SearchResultBean(resultElements[i].getTitle(), resultElements[i].getURL(), resultElements[i].getSnippet()));
                        }
                } catch (GoogleSearchFault f) {
                        throw new IOException("The call to the Google Web APIs failed: " + f.getMessage());
                } catch (Exception e) {
                        throw new IOException("Thread Problem.");
                }
                
                return searchResults;
        }
}

__________________________________________________________________________

返回搜索结果的Bean:SearchResultBean

/**
* Result’s Bean.
*
* @author Jose Sandoval
*/
public class SearchResultBean {
    private String title;
    private String URL;
    private String snippet;
    
    public SearchResultBean(String title, String URL, String summary) {
                super();
                this.title = title;
                this.URL = URL;
                this.snippet = summary;
        }

        public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getURL() {
        return URL;
    }
    
    public void setURL(String url) {
        URL = url;
    }

    public String getSnippet() {
        return snippet;
    }
        
    public void setSnippet(String summary) {
        this.snippet = summary;
    }
}

客户服务中心信箱:[email protected] [email protected] 网站地图

声明

合作伙伴: