Ajax学习

Ajax--Asynchronous Javascript And Xml
局部刷新页面、不能后退
Ajax最早是被Google采用的
最常用的的案例:百度地图
Ajax 中最重要的一个对象是XMLHttpRequest
使用Ajax准备向服务器端发送请求:

xmlHttpRequest.open("GET", "AjaxServlet", true);

这是我们的前台页面:

<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript">
var xmlHttpRequest = null;
function display() {
if (window.ActiveXObject) {
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttpRequest = new XMLHttpRequest();
}
if (null != xmlHttpRequest) {
xmlHttpRequest.open("GET", "AjaxServlet", true);
xmlHttpRequest.onreadystatechange = callBack;
xmlHttpRequest.send(null);
}
}
function callBack() {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
var responseText = xmlHttpRequest.responseText;
document.getElementById("div1").innerHTML = responseText;
}
}
}
</script>

</head>


<body>
<input type="button" value="获取文字" οnclick="display();" />
<br>
<div id="div1"></div>
</body>
</html>


后台的servlet,向前台打印一个Hello,World:

package com.test.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class AjaxServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
System.out.println("doGet invoked!");
out.print("hello,World!");
out.flush();
}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值