简体   繁体   中英

Web search through Servlet in JSP

I am building up a page in JSP with a form to where there is a textbox to insert search query. When the search button is clicked it has to pass into a servlet which collects the terms from textbox and submit it to Google and then collects back the result and display into my own page (say, just below the form in the page before).
Could anyone suggest me if it is possible to pass the search parameter to google programatically from the servlet?

Any insight, suggestion, or psuedo-code sample would be highly appreciated.

Thank You.

Yes it is possible though the Google Custom Search API check out the overview and the instructions below:

Overview

Using REST

Java Library and Guide

I think more or less this is what you are looking for:

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>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM