zoukankan      html  css  js  c++  java
  • GET实现搜索引擎

      首先,在HTML中使用FORM提交数据。当FORM的method属性设置为GET时,浏览器以GET方式提交表单数据。FORM的action属性设置数据将提交到哪个URL。

     1   <body>
    2 <div align = "center">
    3 <img src = "http://www.logostage.com/logos/Google.png" style = 'margin: 25px;' width="368" height="186">
    4 <div>
    5 <form action = '/servlet/servlet/SearchServlet' method = 'get'>
    6 <input type = "radio" name = "type" value = "web" checked>网页
    7 <input type = "radio" name = "type" value = "news">新闻
    8 <input type = "radio" name = "type" value = "image">图片
    9 <input type = "radio" name = "type" value = "video">视频
    10 <br/><br/>
    11 <input type = "text" name = "word" value = "" style = "300px;"><br/><br/>
    12 <input type = "submit" value= "Google一下" style = "150px;">
    13 </form>
    14 </div>
    15 <div style = "margin-top:50px;">&copy; Clara 2011</div>
    18 </div>
    20 </body>

      这段HTML代码的页面效果如下图。好吧,我山寨了><~


    搜索主界面

      然后写一个SearchServlet的类来实现业务逻辑。这里我用了yahoo的search API,不过是下的老版本,现在yahoo搞了个BOSS,要收钱。用的是yahoo_search-2.0.1.jar,需要添加到项目的library里面去,即/WEB-INF/lib下。这里有个问题,直接加到目录底下还是不会找到,需要右键jar包,具体见下图。

    图片搜索的结果如下:

    搜索部分的关键代码:

    public class SearchServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            response.setCharacterEncoding("UTF-8");
            request.setCharacterEncoding("UTF-8");
            
            // 搜索关键字
            String word = request.getParameter("word");
            // 搜索类型
            String type = request.getParameter("type");
            
            response.setContentType("text/html");
             .....
             .....
            
            SearchClient client = new SearchClient("javasdktest");
            
            try{
                if("image".equals(type)){
                    ImageSearchRequest searchRequest = new ImageSearchRequest(URLEncoder.encode(word, "UTF-8"));
                    // 查询记录数
                    searchRequest.setResults(20);
                    // 从第 0 条记录开始显示
                    searchRequest.setStart(BigInteger.valueOf(0));
                    
                    double startTime = System.currentTimeMillis();
                    ImageSearchResults results = client.imageSearch(searchRequest);
                    double endTime = System.currentTimeMillis();
    
                    out.println("<div align=right style='100%; background: #FFDDDD; height:25px; padding:2px; border-top:1px solid #FF9999; margin-bottom:5px; '>");
                    out.println("    总共 " + results.getTotalResultsAvailable() + " 条数据,总用时 " + ( endTime - startTime )/1000 + " 秒。");
                    out.println("</div>");
                    
                    for(ImageSearchResult result : results.listResults()){
                        out.println("<div class='imgDiv'>");
                        out.println("    <div align='center'><a href=\"" + result.getClickUrl() + "\" target=_blank><img width=160 height=120 src=\"" + result.getThumbnail().getUrl() + "\" border='0'></a></div>");
                        out.println("    <div align='center'><a href=\"" + result.getRefererUrl() + "\" target=_blank>" + result.getTitle() + "</a></div>");
                        out.println("    <div align='center'>" + result.getWidth() + "x" + result.getHeight() + " " + result.getFileFormat() + "</div>");
                        out.println("    <div>" + (result.getSummary()==null ? "" : result.getSummary()) + "</div>");
                        out.println("</div>");
                    }
                }   
        else if("web".equals(type)){
        }
        else .......
    } 
  • 相关阅读:
    Newtonsoft.Json(Json.Net)学习笔记
    SQL Server 自动化运维系列
    WCF 动态调用(动态创建实例接口)
    SQL Server 自动化运维系列
    SQL Server 表水平分区
    WCF服务全局异常处理机制
    WCF 透明代理
    WCF扩展记录服务调用时间
    WCF身份验证三:自定义身份验证之<MessageHeader>
    用单链表实现算法2.1
  • 原文地址:https://www.cnblogs.com/clara/p/2108721.html
Copyright © 2011-2022 走看看