ajax应用的基本流程
1、从web表单中获取需要的数据
2 、建立到服务器的url
3 、打开到服务器的连接
4、 设置服务器在完成后要运行的函数
5 、发送请求
ajax.html
<html>
<head>
<script src="selectcustomer.js"></script>
</head>
<form name="form">
select a customer
<select name="customers" onchange="showCustomer(document.form.customers.options[document.form.customers.selectedIndex].text)">
<option value="1">000100</option>
<option value="2">000101</option>
<option value="3">000102</option>
</select>
</form>
<p>
<div id="txtHint"><b>Customer info will be listed here.</b></div>
</p>
</html>
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.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if(xmlHttp.readyState==4||xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var objXMLHttp=null;
if(window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}
ajax.jsp
<%@page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<html>
<body>
<%
String sql=(String)request.getParameter("q");
//out.println(q);
String str="select * from customers where customerid='"+sql+"'";
//String str1="select * from customers";
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery(str);
%>
<table>
<%
while(rst.next())
{
%>
<tr>
<td><%=rst.getString("customerid")%></td>
<td><%=rst.getString("customername")%></td>
<td><%=rst.getString("companyname")%></td>
<td><%=rst.getString("contactname")%></td>
<td><%=rst.getString("address")%></td>
<td><%=rst.getString("postalcode")%></td>
</tr>
<% }
//关闭连接、释放资源
rst.close();
stmt.close();
con.close();
%>
</table>
</body>
</html>
分享到:
相关推荐
Jquery+Ajax+Php+Mysql异步刷新 表单验证
AJAX+jsp无刷新验证码实例AJAX+jsp无刷新验证码实例AJAX+jsp无刷新验证码实例
ajax+jsp网页时时刷新提示. 有详细的注释方便学习
Ajax+jsp无刷新验证码实例 包含 :Ajax+jsp无刷新验证码实例.docx img.jsp net.js num.jsp random.jsp
利用ajax+jsp技术进行局部页面刷新
+Ajax异步无刷新登录实例 本人收藏了3年的资源 现放出 都是总结了很多系统 软件项目实施过程中的经验的 慢慢积累的
AJAX + jsp无刷新聊天室代码.rar AJAX + jsp无刷新聊天室代码.rar
《Ajax+JSP网站开发从入门到精通》的源代码,全书共分为16章,包括JSP的基础知识、JSP访问外部数据、JSP高级技术、Ajax概述、Ajax相关技术、Ajax开发基础、Ajax开发模式、Ajax框架、Ajax常用技巧。本书最后介绍了6个...
JSP中使用Ajax完美实现异步无刷新数据分页
ajax + asp无刷新文件异步上传程序,并有进度条实时显示上传进度!ajax + asp无刷新文件异步上传程序,并有进度条实时显示上传进度!ajax + asp无刷新文件异步上传程序,并有进度条实时显示上传进度!ajax + asp无...
jQuery+Ajax+PHP无刷新分页
struts2+ajax+jquery异步批量上传超大文件.zip struts2+ajax+jquery异步批量上传超大文件.zip struts2+ajax+jquery异步批量上传超大文件.zip struts2+ajax+jquery异步批量上传超大文件.zip struts2+ajax+jquery异步...
AJAX+ASP.NET无刷新提交读取数据实例。
ajax+jsp草稿自动保存
Ajax+ASP.NET无刷新聊天工具 Ajax+ASP.NET无刷新聊天工具