Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
在Java中检索Google的搜索结果

作者:未知 来源:月光软件站 加入时间:2005-5-13 月光软件站

客户端的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;
    }
}

来自:辣妹子社区-Java技术论坛




相关文章

相关软件