模拟百度通过关键字搜索&&ajax
2017-07-20 00:00
441 查看
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>百度搜索</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-2.1.4.js"></script> </head> <body> <center> <div> <h1>巴莫搜索</h1> <div> <input name="keywords" id="kid"><input type="submit" value="巴莫一下"> </div> <div id="did" style=" border: 1px dashed red;width: 143px;position: relative;left: -37px;display: none"></div> </div> </center> </body> <script type="text/javascript"> //加载 $(function(){ //派发事件 $("#kid").keyup(function(){ //获取用户输入的关键字 var $keywords = $(this).val(); if ($keywords != null && $keywords !="") { //清空div $("#did").html(""); var url = "${pageContext.request.contextPath}/ajaxSearch"; var param = {"keywords":$keywords}; $.post(url,param,function(data){ //不为空的时候切割字符串 if (data != null) { $(data).each(function(index,n){ $("#did").append("<div>"+n+"</div>"); }) $("#did").show(); } },"json") }else{ //内容为空时,将div隐藏起来 $("#did").hide(); } }); }); </script> </html>
public class AjaxSearchServlet extends HttpServlet { private static final long serialVersionUID = -3112366969977240031L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求参数 String keywords = request.getParameter("keywords"); //System.out.println(keywords); try { List<Object> list = new SearchService().findKeywords4Ajax(keywords); //System.out.println(list.toString()); if (list != null && list.size() >0 ) {//判断list是否为空 String jsonString = JSON.toJSONString(list); System.out.println(jsonString); response.setCharacterEncoding("utf-8"); response.getWriter().print(jsonString); } } catch (SQLException e) { e.printStackTrace(); } } }
public class SearchService { public List<Object> findKeywords4Ajax(String keywords) throws SQLException { return new SearchDao().findKeywords4Ajax(keywords); } }
public class SearchDao { public List<Object> findKeywords4Ajax(String keywords) throws SQLException { QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "select keywords from t_keywords where keywords like ? limit 5"; //ColumnListHandler:将结果集中某一列的数据存放到List中。 return qr.query(sql, new ColumnListHandler("keywords"),"%"+keywords+"%"); } }
相关文章推荐
- Ajax实现模拟关键字智能匹配搜索效果
- 自动完成技术,像百度一样通过AJAX技术来搜索备选项
- ajax模拟关键字智能匹配搜索效果
- 通过 ajax 仿照百度搜索功能[实例]
- 通过调用关键字来实现测试用例---重复利用百度搜索
- 第95课:通过Spark Streaming的window操作实战模拟新浪微博、百度、京东等热点搜索词案例实战
- 大数据IMF传奇行动绝密课程第95课:通过SparkStreaming的window操作实战模拟新浪微博、百度、京东等热点搜索词案例实战
- 用AJAX,JSON在百度搜索栏内获取联想关键字
- 关于自制网页如何引用百度搜索的关键字菜单~
- JS 动态创建上传表单,通过iframe模拟Ajax 无刷新
- 浅谈asp.net通过本机cookie仿百度(google)实现搜索input框自己主动弹出搜索提示
- Spark Streaming的window操作实战模拟新浪微博、百度、京东等热点搜索词案例实战
- JS JQuery 模拟百度搜索上下键选取
- js模拟百度主页搜索功能
- php+ajax做仿百度搜索下拉自动提示框(有实例)
- php & ajax 小例子——仿百度搜索自动完成下拉框
- python批量关键字百度搜索结果url解码
- 【百度爬虫系列 I】多关键字图片搜索结果汇总
- 通过百度API,搜索地名,实现地图和经纬度的查询
- Python模拟鼠标点击实现方法(将通过实例自动化模拟在360浏览器中自动搜索python)