我正在 JSP 中构建一个页面,其中包含一个用于插入搜索查询的文本框的表单。单击搜索按钮时,它必须传递到一个 servlet,该 servlet 从文本框中收集术语并将其提交给 Google,然后收集结果并显示到我自己的页面中(例如,就在之前页面中的表单下方)。
任何人都可以建议我是否可以从 servlet 以编程方式将搜索参数传递给谷歌?
任何见解、建议或伪代码示例都将受到高度赞赏。
谢谢你。
是的,虽然 Google 自定义搜索 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>
}