我在JSP中构建一个页面,其中有一个表单,其中有一个文本框可以插入搜索查询。单击搜索按钮时,必须将其传递到servlet,该servlet从文本框中收集术语并将其提交给Google,然后收集结果并显示到我自己的页面中(例如,在页面中的表单下方)。登记/> 任何人都可以建议我是否可以从servlet以编程方式将搜索参数传递给谷歌?
任何见解,建议或伪代码样本都将受到高度赞赏。
谢谢。
答案 0 :(得分:3)
是的,虽然Google Custom Search 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>
}