·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » 在Java中检索Google的搜索结果

在Java中检索Google的搜索结果

类别: JAVA教程  评论数:0 总得分:0
  
客户端的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;
    }
}


-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1