Ajax实现简单的google索搜框

来源:互联网 发布:网速监控软件 编辑:程序博客网 时间:2024/05/19 00:43

1. 工具和环境: Eclipse + tomcat + mysql


2, web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <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>GoogleServlet</servlet-name>
    <servlet-class>com.sxt.servlet.GoogleServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>GoogleServlet</servlet-name>
    <url-pattern>/google</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


3,index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  <script type="text/javascript" src="js/jquery-1.7.2.js"></script>
  <script type="text/javascript">
 
  function googles(){
 
  var content=$("#myname").val();
  $("#mybody").empty();
 
  if(content!=""){
  $.post("google",{"content":content},
  function(data){
  var list=data.split("%");
 
  for(var i=0;i<list.length;i++){
  var tr="<tr><td>"+list[i]+"</td></tr>";
  $("#mybody").append(tr);
  }
  }
  )
  }
  }
 
  </script>
   
  </head>
  <body> 
  <label><p>请输入你需要搜索的内容: 比如: 我的世界</p></label><input type="text" id="myname" onkeyup="googles()" style="width: 300px;"/><br/>
     
  <table border="1" style="width: 300px">
  <tbody id="mybody">   
  </tbody> 
  </table> 
  
  </body>
</html>


4, com.servlet 层:


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sxt.bean.contentInfo;
import com.sxt.dao.contentinfoDAO;


@SuppressWarnings("serial")
public class GoogleServlet extends HttpServlet {


private contentinfoDAO condao=new contentinfoDAO();

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


request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;character=UTF-8");

PrintWriter out=response.getWriter();

String name=request.getParameter("content");
List<contentInfo> list=condao.find(name);

String str="";
for (contentInfo cc : list) {
str+=cc.getContent()+"%";

}
if (str.length()>1) {
str=str.substring(0,str.length()-1);


}

out.print(str);

}


}



5. com.dao 层:


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.sxt.bean.contentInfo;
import com.sxt.db.DBManager;

public class contentinfoDAO {


private Connection conn;
private PreparedStatement ps;
private ResultSet rs;

public List<contentInfo> find(String name){//����

List<contentInfo> list=new ArrayList<contentInfo>();

conn = DBManager.getConnection();

String sql = "select *from contentInfo where content like ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,name+"%");
rs = ps.executeQuery();

while(rs.next()){
contentInfo content=new contentInfo();
content.setContentId(rs.getInt(1));
content.setContent(rs.getString(2));
list.add(content);
}

} catch (SQLException e) {
System.out.println(e.getMessage());
} finally{
DBManager.closeConnection(conn, ps, rs);
}
return list;
}


}


6. com.DB 层(传统的DB链接写法):


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Driver;

public class DBManager { 



public static Connection getConnection(){
try {
Class.forName(Driver.class.getName());
} catch (ClassNotFoundException e) {
System.out.println("------------");
}

String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8";
String user = "root";
String password = "root";
Connection conn = null;
try {
conn = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}



public static void closeConnection(Connection conn,PreparedStatement ps,ResultSet rs){
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}



public static void closeConnection(Connection conn,PreparedStatement ps){
try {
ps.close();
conn.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}


7. com.bean :

public class contentInfo {

private Integer contentId;
private String content;
public Integer getContentId() {
return contentId;
}
public void setContentId(Integer contentId) {
this.contentId = contentId;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}


}