前端之家收集整理的这篇文章主要介绍了
ajax搜索提示框,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style> .mouSEOver{ background: #708090; color:#FFFAFA; } .mouSEOut{ background: FFFAFA; color#000000; } </style>
<script type="text/javascript"> function getMoreContens() { var xmlHttp; var content = document.getElementById("keyword").value; if (content == ""){ alert('为空'); clearContent(); return ; } xmlHttp = createXmlHttp(); var url = "servlet?keyword="+content; xmlHttp.open("GET",url,true); xmlHttp.send(null); xmlHttp.onreadystatechange = function callback() { if(xmlHttp.readyState == 4 && xmlHttp.status == 200) { var result = xmlHttp.responseText; var json = eval("("+result+")"); setContent(json) } }; function setContent(contents){ clearContent(); var size = contents.length; for (var i=0; i<size; i++){ var nextNode = contents[i]; var tr = document.createElement("tr"); var td = document.createElement("td"); tr.setAttribute("border",0); tr.setAttribute("bgcolor","#FFFAFA"); td.setAttribute("width","200px"); td.onmouSEOver=function(){ this.className = 'mouSEOver'; }; td.onmouSEOut=function(){ this.className = 'mouSEOut'; }; td.onclick=function(){ }; var text = document.createTextNode(nextNode); td.appendChild(text); tr.appendChild(td); document.getElementById("content_table_body").appendChild(tr); } }; } function clearContent(){ var contentTableBody = document.getElementById("content_table_body"); var size = contentTableBody.childNodes.length; for (var i = size-1; i>=0; i--){ contentTableBody.removeChild(contentTableBody.childNodes[i]); } }; function keywordBlur(){ clearContent(); } function createXmlHttp() { var xmlHttp; if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if(window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); if(!xmlHttp) { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } } return xmlHttp; } </script>
</head>
<body>
<div>
<input type="text" name="keyword" id="keyword" onkeyup="getMoreContens()" onblur="keywordBlur()" onfocus="getMoreContens()">
<button>百度一下</button>
<div id="popDiv""> <table id="content_table" bgcolor="#FFFAFA" border="0" cellspacing="0">
<tbody id="content_table_body">
</tbody>
</table>
</div>
</div>
</body>
</html>
select
package ajax;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
public class Servlet extends HttpServlet {
static List<String> datas = new ArrayList<String>();
static{
datas.add("app");
datas.add("add");
datas.add("boy");
datas.add("book");
datas.add("cat");
datas.add("ccc");
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String keyword = request.getParameter("keyword");
System.out.println("keyword="+keyword);
List<String> listData = getData(keyword);
JSONArray.fromObject(listData);
System.out.println("json"+JSONArray.fromObject(listData));
response.getWriter().write(JSONArray.fromObject(listData).toString());
}
public List<String> getData(String keyword){
List<String> list = new ArrayList<String>();
for (String data:datas){
if (data.contains(keyword)){
list.add(data);
}
}
return list;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ajax</display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Servlet</servlet-name>
<servlet-class>ajax.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
原文链接:https://www.f2er.com/ajax/160956.html