Java Web学习笔记---用GET实现搜索引擎
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Web学习笔记---用GET实现搜索引擎相关的知识,希望对你有一定的参考价值。
今天做了一个实验,关于Servlet的.使用GET实现搜索引擎.因自己没有搜索引擎数据库,所以使用了Yahoo提供的Search API.
浏览效果如下图:
现在雅虎推出了新的Search API---BOSS Search API.把旧的也就是我现在用的API废弃了,导致搜索不到结果.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>search.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <div align="center"> <img src="http://us.i1.yimg.com/us.yimg.com/i/yahoo.gif" alt="Yahoo!" width="147" height="31" border="0" style="margin: 25px; "> <div> <form action="/testServlet/servlet/SearchServlet" method="get"> <input type="radio" name="type" value="web" checked>网页 <input type="radio" name="type" value="news">新闻 <input type="radio" name="type" value="image">图片 <input type="radio" name="type" value="video">视频 <input type="checkbox" name="allowedAdult" value="true">允许成人内容 <br/><br/> <input type="text" name="word" value="" style="width: 300px; "> <input type="submit" value="用雅虎搜索" style="width: 100px; "> </form> </div> <div style="margin-top: 50px; "> © Helloxr 2016-2020 </div> </div> </body> </html>
以上是search.html的代码
下面是Servlet的代码:
package com.helloxr.servlet; import java.io.IOException; import java.io.PrintWriter; import java.math.BigInteger; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.yahoo.search.ImageSearchRequest; import com.yahoo.search.ImageSearchResult; import com.yahoo.search.ImageSearchResults; import com.yahoo.search.NewsSearchRequest; import com.yahoo.search.NewsSearchResult; import com.yahoo.search.NewsSearchResults; import com.yahoo.search.SearchClient; import com.yahoo.search.VideoSearchRequest; import com.yahoo.search.VideoSearchResult; import com.yahoo.search.VideoSearchResults; import com.yahoo.search.WebSearchRequest; import com.yahoo.search.WebSearchResult; import com.yahoo.search.WebSearchResults; public class SearchServlet extends HttpServlet { private static final long serialVersionUID = 98643567L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); String word = request.getParameter("word"); String type = request.getParameter("type"); String allowedAdult = request.getParameter("allowedAdult"); boolean adultOk = "true".equals(allowedAdult); // 是否允许成人内容checkbox的特性,若选中,则为"true",否则,为 null. PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.01 Transitional//EN\\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>" + word + " 搜索结果</TITLE></HEAD>"); out.println(" <BODY>"); out.println("<div style=‘float:left; heigt:40px; ‘><img src=‘http://us.i1.yimg.com/us.yimg.com/i/yahoo.gif‘></div>"); out.println("<form action=‘" + request.getRequestURI() + "‘ method=‘get‘>"); out.println(" <div style=‘height:40px; ‘>"); out.println(" <input type=‘radio‘ name=‘type‘ value=‘web‘ " + (type.equals("web")?"checked":"") + ">网页"); out.println(" <input type=‘radio‘ name=‘type‘ value=‘news‘ " + (type.equals("news")?"checked":"") + ">新闻"); out.println(" <input type=‘radio‘ name=‘type‘ value=‘image‘ " + (type.equals("image")?"checked":"") + ">图片"); out.println(" <input type=‘radio‘ name=‘type‘ value=‘video‘ " + (type.equals("video")?"checked":"") + ">视频"); out.println(" "); out.println(" <input type=‘checkbox‘ name=‘allowedAdult‘ value=‘true‘ " + (adultOk?"checked":"" ) + ">允许成人内容 <br/>"); out.println(" <input type=‘text‘ name=‘word‘ value=‘" + word + "‘ style=‘width:300px; ‘> <input type=‘submit‘ value=‘用雅虎搜索‘ style=‘width:100px; ‘>"); out.println("</div>"); out.println("</form>"); SearchClient client = new SearchClient("javasdktest"); try{ if("image".equals(type)){ ImageSearchRequest searchRequest = new ImageSearchRequest(URLEncoder.encode(word, "UTF-8")); searchRequest.setAdultOk(adultOk); searchRequest.setResults(20); searchRequest.setStart(BigInteger.valueOf(0)); double startTime = System.currentTimeMillis(); ImageSearchResults results = client.imageSearch(searchRequest); double endTime = System.currentTimeMillis(); out.println("<div align=right style=‘width: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)){ WebSearchRequest searchRequest = new WebSearchRequest(URLEncoder.encode(word, "UTF-8")); // 是否显示成人内容 searchRequest.setAdultOk(adultOk); // 查询记录数 searchRequest.setResults(20); // 从第 0 条记录开始显示 searchRequest.setStart(BigInteger.valueOf(0)); double startTime = System.currentTimeMillis(); WebSearchResults results = client.webSearch(searchRequest); double endTime = System.currentTimeMillis(); out.println("<div align=right style=‘width: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(WebSearchResult result : results.listResults()){ out.println("<div style=‘margin:8px; width:500px; ‘>"); out.println(" <div><a href=\\"" + result.getClickUrl() + "\\" target=_blank><b>" + result.getTitle() + "</b></a> 文件格式:" + result.getMimeType() + "</div>"); out.println(" <div>网址:<a href=\\"" + result.getUrl() + "\\" target=_blank>" + result.getUrl() + "</a></div>"); out.println(" <div>" + result.getSummary() + (result.getCache()==null ? "" : " [<a href=\\"" + result.getCache().getUrl() + "\\" target=_blank>网页快照</a>]") +"</div>"); out.println("</div>"); } } else if("news".equals(type)){ NewsSearchRequest searchRequest = new NewsSearchRequest(URLEncoder.encode(word, "UTF-8")); // 是否显示成人内容 // searchRequest.setAdultOk(adultOk); // 查询记录数 searchRequest.setResults(20); // 从第 0 条记录开始显示 searchRequest.setStart(BigInteger.valueOf(0)); double startTime = System.currentTimeMillis(); NewsSearchResults results = client.newsSearch(searchRequest); double endTime = System.currentTimeMillis(); out.println("<div align=right style=‘width: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(NewsSearchResult result : results.listResults()){ out.println("<div style=‘margin:8px; width:500px; ‘>"); out.println(" <div><a href=\\"" + result.getClickUrl() + "\\" target=_blank><b>" + result.getTitle() + "</b></a></div>"); out.println(" <div>网址:<a href=\\"" + result.getUrl() + "\\" target=_blank>" + result.getUrl() + "</a></div>"); out.println(" <div>" + result.getSummary() + "</div>"); out.println("</div>"); } } else if("video".equals(type)){ VideoSearchRequest searchRequest = new VideoSearchRequest(URLEncoder.encode(word, "UTF-8")); // 是否显示成人内容 searchRequest.setAdultOk(adultOk); // 查询记录数 searchRequest.setResults(20); // 从第 0 条记录开始显示 searchRequest.setStart(BigInteger.valueOf(0)); double startTime = System.currentTimeMillis(); VideoSearchResults results = client.videoSearch(searchRequest); double endTime = System.currentTimeMillis(); out.println("<div align=right style=‘width: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(VideoSearchResult 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>"); } } }catch(Exception e){ e.printStackTrace(); } out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
以上是关于Java Web学习笔记---用GET实现搜索引擎的主要内容,如果未能解决你的问题,请参考以下文章
java web 学习笔记 - jsp用的文件上传组件 SmartUpload
[原创]java WEB学习笔记79:Hibernate学习之路--- 四种对象的状态,session核心方法:save()方法,persist()方法,get() 和 load() 方法,updat