今天在W3C school 上看了看ajax.
再结合之前学过的东西外加google.写这么篇文章.
算是javaeye 借我一张草纸,我草稿写在这里.
ajax.html ajax.jsp selectcustomer.js 这几个文件保存在eclipse工程webroot
同一文件夹(AJAXmysql)下. 在工程里面导入mysql的jar包.
ajax.html
<html>
<head>
<script src="selectcustomer.js"></script>
</head>
<body>
<center>
<form name="form">
选择顾客ID:
<select name="customers" οnchange="showCustomer(this.value)">
<option value="24">000100</option>
<option value="69">000101</option>
<option value="73">000102</option>
<option value="74">000103</option>
<option value="75">000104</option>
<option value="80">000105</option>
<option value="81">000106</option>
<option value="82">000107</option>
</select>
</form>
<b>ID :<span id="id"></span></b><br/>
姓名 :<span id="name"></span><br/>
密码 :<span id="password"></span><br/>
年龄 :<span id="age"></span><br/>
头像 :<img id = "head_url">
</center>
</body>
</html>
ajax.jsp
<%@page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<%
String sql=(String)request.getParameter("q");
String str="select id,name,password,age,head_url from users where id = "+sql;
//String str1="select * from customers";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/rails","root","123456");
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery(str);
while(rst.next())
{
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
out.println("<company>");
out.println("<id>" + rst.getString("id")+ "</id>");
out.println("<name>" + rst.getString("name")+ "</name>");
out.println("<password>" + rst.getString("password")+ "</password>");
out.println("<age>" +rst.getString("age")+ "</age>");
out.println("<head_url>" +rst.getString("head_url")+ "</head_url>");
out.println("</company>");
}
//关闭连接、释放资源
rst.close();
stmt.close();
con.close();
%>
selectcustomer.js
var xmlHttp;
function showCustomer(str)
{
xmlHttp=GetXmlHttpObject();
if(xmlHttp==null)
{
alert("Browser does not support HTTP Request");
return ;
}
var url="ajax.jsp";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=stateChanged;
}
function stateChanged()
{
if(xmlHttp.readyState==4||xmlHttp.readyState=="complete")
{
//id,name,password,age,head_url
var xmlDoc=xmlHttp.responseXML.documentElement;
document.getElementById("id").innerHTML= xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
document.getElementById("name").innerHTML= xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
document.getElementById("password").innerHTML= xmlDoc.getElementsByTagName("password")[0].childNodes[0].nodeValue;
document.getElementById("age").innerHTML= xmlDoc.getElementsByTagName("age")[0].childNodes[0].nodeValue;
document.getElementById("head_url").src= xmlDoc.getElementsByTagName("head_url")[0].childNodes[0].nodeValue;
}
}
function GetXmlHttpObject()
{
var objXMLHttp=null;
if(window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}
数据库用的是mysql. 表,数据,字段什么的都很简单.