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输入框的内容 */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;}//回调函数function callback () { if(xmlHttp.readyState==4){ if(xmlHttp.status==200){ var result=xmlHttp.responseText; alert(result); var json=eval("("+result+")"); alert(json); } }}//设置关联数据的显示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); }}
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);
}
}
/**
* 全局变量
*/
var xmlHttp;
/**
* 得到input输入框的内容
*/
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;
}
//回调函数
function callback () {
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var result=xmlHttp.responseText;
alert(result);
var json=eval("("+result+")");
alert(json);
}
}
}
//设置关联数据的显示
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);
}
}