学习了Ajax技术,写了一个最简单的Ajax应用

<%@page contentType="text/html; charset=utf-8" language="java" import="java.sql.* " errorPage="" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'myHelloWorld.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> <script type="text/javascript">
function ok(){
var xmlhttp;
if(window.XMLHttpRequest){
//非ie
xmlhttp=new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
//ie
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
//设定回调函数
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
//获取返回值
var msgWelcome=xmlhttp.responseText;
var msg=document.getElementById("msg");
msg.innerHTML=msgWelcome;
}
}
}
//设定请求
xmlhttp.open("get", "http://localhost:8080/LearnJava/myHelloWorld.do", true);
//设定http头
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//发送请求
xmlhttp.send(null);
}
</script>
</head> <body>
<span id="msg"></span><br>
<input type="button" onclick="ok()" value="单击">
</body>
</html>

关键代码:

<script type="text/javascript">
function ok(){
var xmlhttp;
if(window.XMLHttpRequest){
//非ie
xmlhttp=new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
//ie
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
//设定回调函数
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
//获取返回值
var msgWelcome=xmlhttp.responseText;//获取response的输出值
var msg=document.getElementById("msg");
msg.innerHTML=msgWelcome;
}
}
}
//设定请求
xmlhttp.open("get", "http://localhost:8080/LearnJava/myHelloWorld.do", true);
//设定http头
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//发送请求
xmlhttp.send(null);
}
</script>

这里只是一个简单的ajax的应用,将servlet输出信息写入页面中。过程是:

首先创建一个xmlhttp对象,然后以get方法向servlet发送请求。当

onreadystatechange事件发生,则调用回调函数,向界面输出信息。

最新文章

  1. Angular JS 学习之路由
  2. Codeforces #380 div2 D(729D) Sea Battle
  3. python 3.5.2安装mysql驱动报错
  4. CUBRID学习笔记 30 复制表结构 cubrid教程
  5. C/C++ 如何劫持别人家的命令||函数||程序(只能对于window而言)
  6. HDU4539+状态压缩DP
  7. 微信原生支付 Native扫码支付( V3.3.7 版本)
  8. [UWP小白日记-4]记账项目-2
  9. resin远程调试配置,connection refused问题解决
  10. 【阿里聚安全&#183;安全周刊】Python库现后门 可窃取用户SSH信息|Facebook再曝300万用户数据泄露
  11. [nodejs] nodejs开发个人博客(一)准备工作
  12. [IOI2014]holiday假期(分治+主席树)
  13. Dota 2 中安装包的作用
  14. ZBrush常用3D术语
  15. 跟我学SharePoint 2013视频培训课程——签出、签入文档(9)
  16. http 同步异步请求
  17. PHP mysqli 增强 批量执行sql 语句的实现代码
  18. C#构造方法(函数)
  19. IDF实验室—不难不易的js加密
  20. SQL Server -&gt;&gt; Enable Instant File Initialization(开启文件及时初始化)

热门文章

  1. Python datetime 格式化字符串:strftime()
  2. PLSQL Developer连接远程Oracle方法(非安装client)
  3. Spring框架学习(10)Spring中如何使用事务?
  4. (转)Linux下内存映射文件的用法简介
  5. [Functional Programming Monad] Apply Stateful Computations To Functions (.ap, .liftA2)
  6. Netty利用ChannelGroup广播消息
  7. 使用python语言编写脚本控制freeswitch总结
  8. webDriver API——第14部分Color Support
  9. React中静态类型校验 - PropTypes
  10. Java Collection之Queue具体解释及用途