0
我在JSP中用一種形式在頁面中建立一個頁面,以便在其中插入搜索查詢。當點擊搜索按鈕時,它必須傳入一個servlet,它從文本框中收集條件並將其提交給Google,然後收集結果並顯示到我自己的頁面中(比如,在頁面之前的表格之下)。
任何人都可以建議我,如果有可能將搜索參數傳遞給谷歌編程從servlet?通過JSP中的Servlet進行網頁搜索
任何洞察力,建議,或僞代碼示例將不勝感激。
謝謝。
我在JSP中用一種形式在頁面中建立一個頁面,以便在其中插入搜索查詢。當點擊搜索按鈕時,它必須傳入一個servlet,它從文本框中收集條件並將其提交給Google,然後收集結果並顯示到我自己的頁面中(比如,在頁面之前的表格之下)。
任何人都可以建議我,如果有可能將搜索參數傳遞給谷歌編程從servlet?通過JSP中的Servlet進行網頁搜索
任何洞察力,建議,或僞代碼示例將不勝感激。
謝謝。
是有可能雖然谷歌自定義搜索API檢查出的概述和下面的說明:
我覺得或多或少,這是什麼您正在查找:
package com.hahahaha.servlet;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.customsearch.Customsearch;
import com.google.api.services.customsearch.model.Result;
import com.google.api.services.customsearch.model.Search;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author hahahahaha
*/
@WebServlet(name = "GoogleSearchServlet", urlPatterns = {"/GoogleSearchServlet"})
public class GoogleSearchServlet extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet GoogleSearchServlet</title>");
out.println("</head>");
out.println("<body>");
// Get your query string posted by the user
String query = request.getParameter("query");
//Instantiate a Customsearch object using NetHttpTransport and the JacksonFactory (JSON library)
Customsearch customsearch = new Customsearch(new NetHttpTransport(), new JacksonFactory());
// Instantiate a Customsearch.Cse.List object using your query string
com.google.api.services.customsearch.Customsearch.Cse.List list = customsearch.cse().list(query);
// Set your API KEY
list.setKey("YOUR_API_KEY");
// Set your custom search engine ID
list.setCx("YOUR_CSEID");
// Execute the search
Search results = list.execute();
// Get the result items
List<Result> items = results.getItems();
// Loop through your result items and stream them to the client
for(Result result : items){
out.println("<b>" + result.getHtmlTitle() + "</b>");
}
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
感謝您的回覆。不過,我發現它允許每天免費版中有限數量的查詢。現在我正在看你的聯繫和學習。我只實施演示項目,所以它可能會有用。 – user1382329
我知道它每天只允許有限數量的查詢,但它就是這樣。我沒有強加這個限制...... Google做到了。我相信我回答了我的朋友的問題,所以請接受它。 – DaTroop