天天看点

JQ仿百度提示页面

JQ仿百度提示页面

代码实现

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'search.jsp' starting page</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">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css" target="_blank" rel="external nofollow" >
	-->

<script type="text/javascript" src="jquery-3.2.1.js"></script>
</head>
<script type="text/javascript">
	$(function() {
		$("#word").keyup(function() {
			/*   获得文本框的值 */
			var word = $("#word").val();
			/* 异步发送请求 */
			if (word != "") {
				$.post("/J2EE/searchservlet", {
					"word" : word
				}, function(data) {
				
					$("#d1").show().html(data);
				});
			} else {
			$("#d1").hide();
			}
		});
	});
</script>
<body>
	<center>
		<h1>百度一下</h1>
		<input type="text" name="word" id="word"
			style="width: 400px;height: 25px" /><input type="button"
			value="百度一下" /> <br><span id="d1"></span>
	</center>
</body>
</html>
           
package search;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




public class searchservlet extends HttpServlet {


	/**
	 * 仿百度搜索页面
	 */
	private static final long serialVersionUID = 1L;




	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");//解决post提交中文乱码
		response.setContentType("UTF-8");//解决页面输出中文乱码
		String word=request.getParameter("word");
		wordservice ws=new wordservice();
		try {
			List<Words> words=ws.search(word);
			request.setAttribute("words", words);
			request.getRequestDispatcher("/html/info.jsp").forward(request, response);//牢记转发时不要带工程名
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}




	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet( request, response);


	}


}
           
package search;

public class Words {
	private int id;
	private String word;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getWord() {
		return word;
	}
	public void setWord(String word) {
		this.word = word;
	}

}
           
package search;

import java.sql.SQLException;
import java.util.List;

public class wordservice {

	public List<Words> search(String word) throws SQLException {
		// TODO Auto-generated method stub
		worddao wd=new worddao();
		return wd.search(word);
	}



}
           
package search;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import utils.c3p0tool;

public class worddao {

	public List<Words> search(String word) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner qr=new QueryRunner(c3p0tool.getDataSource());
		/*String sql1="insert into words values(null,?)";
		qr.update(sql1,word);*/
		String sql="select * from words where word like ? limit 2";/*只查两个*/
		List<Words> l = qr.query(sql,new BeanListHandler<Words>(Words.class),word+"%");
		for(Words i:l){
			String sql1="insert into words values(null,?)";
			qr.update(sql1,i.getWord());
		}
		return l;

	}

}
           
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table>
	<c:forEach items="${words}" var="w">
		<tr>
			<td>${w.word}</td>
		</tr>
	</c:forEach>
</table>