欢迎光临
我们一直在努力

在Java中检索Google的搜索结果-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在Java中检索Google的搜索结果-JSP教程,Java技巧及代码
分享到: 更多 (0)