关于Thread,服务器端每隔2秒不断的响应数据,客户端不间断的得到数据 服务器端每隔2秒不断的响应数据,客户端不间断的得到数据。怎么实现呀。各位帮帮忙啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这样的话,你需要客户端不停地向服务器端发送请求,用ajax可以实现的了,加上javascript的计时器,可以实现这样的功能我之前做过一个这样的项目,就是跟你的要求一样的。用ajax+JavaScript实现对话功能。 刚写的Ajax聊天功能,和你的需求差不多,每隔800毫秒向服务器发送请求!页面代码如下:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%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 'index.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"> <script type="text/javascript"> var xmlHttpRequest; //创建xml对象 function createXmlHttpRequest(){ if(window.XMLHttpRequest){ xmlHttpRequest = new XMLHttpRequest; } else if(window.ActiveXObject){ xmlHttpRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0"); } if(!xmlHttpRequest){ alert("create XMLHttpRequest Object fail."); } } //回调函数 function callback(){ var oldValue = document.getElementById("chatArea").value; if(xmlHttpRequest.readyState==4){ if(xmlHttpRequest.status==200){ //处理逻辑 document.getElementById("chatArea").value = xmlHttpRequest.responseText; }else{ alert("服务器"+xmlHttpRequest.status+"错误"); } } } //和服务器建立连接发送请求 function sendRequest(){ var context = document.getElementById("context").value; createXmlHttpRequest(); xmlHttpRequest.onreadystatechange=callback; xmlHttpRequest.open("post","<%=path%>/chat.do",true); xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttpRequest.send("context="+context); } //定时向服务器发送空消息 function sendEmptyRequest(){ var context = document.getElementById("context").value; createXmlHttpRequest(); xmlHttpRequest.onreadystatechange=callback; xmlHttpRequest.open("post","<%=path%>/chat.do",true); xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttpRequest.send(null); setTimeout("sendEmptyRequest()",800); } </script> </head> <body onload="sendEmptyRequest()"> <textarea name="chatArea" id="chatArea" rows="30" cols="100" readonly="readonly" ></textarea> <p> <input type="text" size="80" name="context" id="context"><input type="button" value="发送" onclick="sendRequest()"> </body></html> 线程?那就一直连接,一直读取数据,一直发送数据好了,没必要每次都连接,接收,断开连接,太浪费了。数据是发送方控制的,可以用定时器向OutputStream输出数据,也可以每个循环都Thread.sleep(2000),休眠2秒。如果真的很固定,建议同定时器。Timer和TimerTask详解 你这个客户端和服务器端是个说明样子的啊b/s吗要是的话ajax+js就可以不过我还没有用现在用的是dwr应该也可以这个还好整点呵呵然后用js的定时任务 客户端其实对于我来说就是手机端,而服务器端其实就是j2ee端,webserver端,手机端不停的从服务器得到数据,而webserver每隔2秒发送数据。 Struts中使用html标签库为什么一直出错? JAVA 社区型消息机制,应该怎么个思路? jfreechart柱状图的一个古怪需求 excel导出问题 对于你来说是简单的问题请进 有大大能帮忙仿下这个JS折叠菜单吗? 怎样获得文件夹的大小? 菜鸟问题!送分! 对于防止低级错误如:变量名写错,漏括号等有什么好的建议?(给分) Select 后面再增加个Select问题,在线等,告急 !!! jps运行看不到运行结果。。 根据不同用户信息显示不通页面内容
我之前做过一个这样的项目,就是跟你的要求一样的。
用ajax+JavaScript实现对话功能。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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">
<script type="text/javascript">
var xmlHttpRequest;
//创建xml对象
function createXmlHttpRequest(){
if(window.XMLHttpRequest){
xmlHttpRequest = new XMLHttpRequest;
} else if(window.ActiveXObject){
xmlHttpRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
if(!xmlHttpRequest){
alert("create XMLHttpRequest Object fail.");
}
}
//回调函数
function callback(){
var oldValue = document.getElementById("chatArea").value;
if(xmlHttpRequest.readyState==4){
if(xmlHttpRequest.status==200){
//处理逻辑
document.getElementById("chatArea").value = xmlHttpRequest.responseText;
}else{
alert("服务器"+xmlHttpRequest.status+"错误");
}
}
}
//和服务器建立连接发送请求
function sendRequest(){
var context = document.getElementById("context").value;
createXmlHttpRequest();
xmlHttpRequest.onreadystatechange=callback;
xmlHttpRequest.open("post","<%=path%>/chat.do",true);
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttpRequest.send("context="+context);
}
//定时向服务器发送空消息
function sendEmptyRequest(){
var context = document.getElementById("context").value;
createXmlHttpRequest();
xmlHttpRequest.onreadystatechange=callback;
xmlHttpRequest.open("post","<%=path%>/chat.do",true);
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttpRequest.send(null);
setTimeout("sendEmptyRequest()",800);
}
</script>
</head>
<body onload="sendEmptyRequest()">
<textarea name="chatArea" id="chatArea" rows="30" cols="100" readonly="readonly" ></textarea>
<p>
<input type="text" size="80" name="context" id="context"><input type="button" value="发送" onclick="sendRequest()">
</body>
</html>
b/s吗要是的话ajax+js就可以
不过我还没有用
现在用的是dwr应该也可以
这个还好整点
呵呵
然后用js的定时任务