您的位置:首页 > 其它

模拟百度通过关键字搜索&&ajax

2017-07-20 00:00 399 查看
<%@ 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+"%");
}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息