protected?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException?{request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");String?keyWord=request.getParameter("keyWord");ajax?ajax=new?ajax();ajax.setContent(keyWord);List<ajax>?list=as.getAjax(ajax);List<String>?strings=new?ArrayList<String>();for?(ajax?str?:?list)?{strings.add(str.getContent());}System.out.println(JSONArray.fromObject(strings).toString());response.getWriter().write(JSONArray.fromObject(strings).toString());}
/**?*?全局變量?*/var?xmlHttp;/**?*?得到input輸入框的內(nèi)容?*/function?getMoreContents()?{ var?content=document.getElementById('input'); if(content.value==""){ return; } //?alert(content.value); xmlHttp=createXMLHttp(); //?alert(xmlHttp); var?url="search?keyWord="+escape(content.value); xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange=callback; xmlHttp.send(null);}//獲得xmlHttp對象function?createXMLHttp?()?{ var?xmlHttp; if(window.XMLHttpRequest){ xmlHttp=new?XMLHttpRequest(); } if(window.ActiveXObject){ xmlHttp=new?ActiveXObject("Microsoft.XMLHttp"); if(!xmlHttp){ xmlHttp=new?ActiveXObject("Msxml2.XMLHttp"); } } return?xmlHttp;}//回調(diào)函數(shù)function?callback?()?{ if(xmlHttp.readyState==4){ if(xmlHttp.status==200){ var?result=xmlHttp.responseText; alert(result); var?json=eval("("+result+")"); alert(json); } }}//設(shè)置關(guān)聯(lián)數(shù)據(jù)的顯示function?setContent(contents){ 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"); td.setAttribute("bgcolor","aliceblue"); 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('contentTableBody').appendChild(tr); }}
2019-08-24
package Servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Service.AjaxService;
import Service.AjaxServiceImpl;
import entity.ajax;
import net.sf.json.JSONArray;
@WebServlet("/search")
public class searchServlet extends HttpServlet {
private AjaxService as;
private static final long serialVersionUID = 1L;
? ? ? ?
? ?
? ? public searchServlet() {
? ? as=new AjaxServiceImpl();
? ? }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String keyWord=request.getParameter("keyWord");
ajax ajax=new ajax();
ajax.setContent(keyWord);
List<ajax> list=as.getAjax(ajax);
List<String> strings=new ArrayList<String>();
for (ajax str : list) {
strings.add(str.getContent());
}
System.out.println(JSONArray.fromObject(strings).toString());
response.getWriter().write(JSONArray.fromObject(strings).toString());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
2019-08-24
/**
?* 全局變量
?*/
var xmlHttp;
/**
?* 得到input輸入框的內(nèi)容
?*/
function getMoreContents() {
var content=document.getElementById('input');
if(content.value==""){
return;
}
// alert(content.value);
xmlHttp=createXMLHttp();
// alert(xmlHttp);
var url="search?keyWord="+escape(content.value);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.send(null);
}
//獲得xmlHttp對象
function createXMLHttp () {
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHttp");
if(!xmlHttp){
xmlHttp=new ActiveXObject("Msxml2.XMLHttp");
}
}
return xmlHttp;
}
//回調(diào)函數(shù)
function callback () {
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var result=xmlHttp.responseText;
alert(result);
var json=eval("("+result+")");
alert(json);
}
}
}
//設(shè)置關(guān)聯(lián)數(shù)據(jù)的顯示
function setContent(contents){
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");
td.setAttribute("bgcolor","aliceblue");
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('contentTableBody').appendChild(tr);
}
}