WEB开发网
开发学院WEB开发Jsp 在Java中检索Google的搜索结果 阅读

在Java中检索Google的搜索结果

 2008-01-05 09:39:45 来源:WEB开发网   
核心提示: 客户端的javascript: <script language="javascript"> var theURL = "/examples/servlet/MyGoogleSearchServlet?search="; var http = get
 
客户端的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 IOExceptio

Tags:Java 检索 Google

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接