struts ajax应用二 自动更新获取服务器数据
2009-05-15 14:44
495 查看
autoupdate.jsp:
< html xmlns = "http://www.w3.org/1999/xhtml" > <head > <meta http - equiv = "Content-Type"
content = "text/html; charset=UTF-8" / ><title > Auto Update < /title>
</head > <script type = "text/javascript" >
var xmlHttp;
//创建XMLHttp对象实例
function createXMLHttpRequest() {
if (window.ActiveXObject) { //如果是IE
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function doStart() {
createXMLHttpRequest();
var url = "autoupdate.do";
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = startcallback;
xmlHttp.send(null);
}
function startcallback() {
if (xmlHttp.readyState == 4) { //判断请求的状态(0=未初始化,1=正在加载,2=已经加载,3=交互,4=完成)
if (xmlHttp.status == 200) { //判断Server的Http状态码(200对应 ok , 404对应Not Found,204对应返回空)
setInterval("doStart()", 3000); //设置循环调用
refreshTime(xmlHttp.responseText);
}
}
}
function refreshTime(Textstr) {
var timeobj = document.getElementById("showtime");
timeobj.value = Textstr;
}
< /script>
<body onload="doStart()">
<input type="text" id="showtime" / >
<input type = "button" id = "testbtn" value = "Click me"onclick = "javascript:refreshTime('cxm come')" / >
</body>
</html >
Server Action:
package cxm.ajax;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import java.io.PrintWriter;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class autoupdate extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
PrintWriter pout = response.getWriter();
pout.print(getStringDate());
System.out.println("运行了");
pout.flush();
pout.close();
return null;
}
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
}
< html xmlns = "http://www.w3.org/1999/xhtml" > <head > <meta http - equiv = "Content-Type"
content = "text/html; charset=UTF-8" / ><title > Auto Update < /title>
</head > <script type = "text/javascript" >
var xmlHttp;
//创建XMLHttp对象实例
function createXMLHttpRequest() {
if (window.ActiveXObject) { //如果是IE
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function doStart() {
createXMLHttpRequest();
var url = "autoupdate.do";
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = startcallback;
xmlHttp.send(null);
}
function startcallback() {
if (xmlHttp.readyState == 4) { //判断请求的状态(0=未初始化,1=正在加载,2=已经加载,3=交互,4=完成)
if (xmlHttp.status == 200) { //判断Server的Http状态码(200对应 ok , 404对应Not Found,204对应返回空)
setInterval("doStart()", 3000); //设置循环调用
refreshTime(xmlHttp.responseText);
}
}
}
function refreshTime(Textstr) {
var timeobj = document.getElementById("showtime");
timeobj.value = Textstr;
}
< /script>
<body onload="doStart()">
<input type="text" id="showtime" / >
<input type = "button" id = "testbtn" value = "Click me"onclick = "javascript:refreshTime('cxm come')" / >
</body>
</html >
Server Action:
package cxm.ajax;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import java.io.PrintWriter;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class autoupdate extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
PrintWriter pout = response.getWriter();
pout.print(getStringDate());
System.out.println("运行了");
pout.flush();
pout.close();
return null;
}
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
}
相关文章推荐
- 通过服务器获取终端上应用软件更新数据的方法和系统
- LoadRunner简单应用之:win/websockets通信中获取服务器返回数据
- Laravel 实现自动更新服务器数据
- 【Android】 -用adb实现自动获取应用性能数据
- 登陆验证姓名密码,自动登录 ,get获取服务器数据,显示表格形式到界面
- EventSource 对象用于接收服务器发送事件通知,是网页自动获取来自服务器的更新
- Android应用开发之获取web服务器xml数据
- [原]as3 flash web 应用 (8)图片批量上传之 向服务器发送和获取额外数据
- RxJava简单使用、模仿访问服务器获取数据更新界面-基于mvp架构
- Android系统下,用adb实现自动获取应用性能数据
- WayOs PPPoE群集服务器:自动同步用户信息,包括增加,更新,同步操作!保证多台服务器数据同步
- Android应用开发之获取web服务器xml数据
- vue单页应用中,使用setInterval()定时向服务器获取数据,后来跳转页面后,发现还在不停的获取数据。
- Andoid应用更新,解决andrid5.0获取系统权限把下载APK存到SD卡中,版本号的比较,有手动更新和自动更新
- linux下svn使用(三) - svn高级应用 - 自动更新web服务器
- Ajax实现定时从服务器获取数据,定时更新数据
- 万能糖果公司web应用——从服务器获取数据
- XUtils访问服务器获取json数据,并用Gson解析json
- UWP 应用获取各类系统、用户信息 (1) - 设备和系统的基本信息、应用包信息、用户数据账户信息和用户账户信息
- 使用git服务器实现生产环境应用服务器代码更新管理的方案