请问怎么让jsp界面上动态显示数据。

小裴同学 2011-12-28 09:50:10
比如说,界面上一直在显示一个时间。动态的走。1秒变化一次。这样的代码怎么写哇。。
嗯,最好有代码,谢谢咯。
我这里是有一个String 这个String是当前的流量。比如180kb/s。我想一秒刷新一次这个数据。我在后台可以得到,就是想怎么在jsp上面显示。
...全文
543 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
小裴同学 2011-12-29
  • 打赏
  • 举报
回复
大家好,我修改了一下代码如下,可是还是不行。。大家帮忙看看错误在哪里。。
XML code

<%@page import="com.network.NetWorkThread"%>
<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据界面</title>
</head>
<jsp:useBean id="Bean0" scope="application"
class="com.codyy.test.GetInfo" />
<jsp:useBean id="Bean1" scope="application"
class="com.network.NetWorkThread" />
<%
new Thread(Bean1).start();
%>
<script>
function createXMLHttpRequest() {
if (window.XMLHttpRequest) { //Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
}
//发送请求函数
function sendRequest() {
createXMLHttpRequest();
var url = "select.jsp";
XMLHttpReq.open("GET", url);
XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
XMLHttpReq.send(null); // 发送请求
}
// 处理返回信息函数
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判断对象状态
if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息
document.getElementById("product").innerText = xmlHttp.responseText
+ "dsadsa";
//document.getElementById("count").innerHTML = xmlHttp.responseText;
setInterval("sendRequest()", 1000)
} else { //页面不正常
window.alert("您所请求的页面有异常。");
}
}
}
window.onunload = function() {

}
window.onbeforeunload = function() {

}
//function DisplayHot() {
//var name = XMLHttpReq.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
//var count = XMLHttpReq.responseXML.getElementsByTagName("count")[0].firstChild.nodeValue;
//document.getElementById("product").innerHTML = name;
//document.getElementById("count").innerHTML = count;
//}
</script>
<body onload=sendRequest()>
<table align="center" style="BORDER-COLLAPSE: collapse"
borderColor=#111111 cellSpacing=0 cellPadding=0 width=500
bgColor=#f5efe7 border=0>
<TR>
<TD align=center bgColor=#dbc2b0 height=19 colspan="2"><B>服务器当前信息</B>
</TD>
</TR>
<tr>
<td height="20">当前连接数为:<%=Bean0.getConnNum()%></td>
<td height="20"></td>
</tr>
<tr>
<td height="20">当前CPU占用率为:<%=Bean0.getCpuInfo()%></td>
<td height="20"></td>
</tr>
<tr>
<td height="20">上传流量:</td>
<td height="20" id="product"></td>
</tr>
<tr>
<td height="20">下载流量:</td>
<td height="20" id="count"></td>
</tr>
</table>
</body>
</html>



<%@page import="com.network.NetWorkThread"%>
<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<jsp:useBean id="Bean0" scope="application"
class="com.codyy.test.GetInfo" />
<jsp:useBean id="Bean1" scope="application"
class="com.network.NetWorkThread" />
<%
//out.println("当前连接数为:" + Bean0.getConnNum() + "");
//out.println("<br>");
//out.println("当前CPU占用率为::" + Bean0.getCpuInfo() + "");
//out.println("<br>");
//out.println("当前流量接收为:" + Bean0.getLiuLiangRececied() + "");
//out.println("当前流量发送为:" + Bean0.getLiuLiangSend() + "");
//out.print(Bean1.getRececied());
out.print("asdasdjasj");
System.out.println("ssssssss");
//out.clearBuffer();
//if (!request.getParameter("gogo").equals("love")) {
// System.out.println("aaaaaaaaaaaa");
// out.print(Bean1.getRececied());
//}
//out.flush();
%>
</html>
小裴同学 2011-12-29
  • 打赏
  • 举报
回复
大家好,我修改了一下代码如下,可是还是不行。。大家帮忙看看错误在哪里。。
XML code

<%@page import="com.network.NetWorkThread"%>
<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据界面</title>
</head>
<jsp:useBean id="Bean0" scope="application"
class="com.codyy.test.GetInfo" />
<jsp:useBean id="Bean1" scope="application"
class="com.network.NetWorkThread" />
<%
new Thread(Bean1).start();
%>
<script>
function createXMLHttpRequest() {
if (window.XMLHttpRequest) { //Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
}
//发送请求函数
function sendRequest() {
createXMLHttpRequest();
var url = "select.jsp";
XMLHttpReq.open("GET", url);
XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
XMLHttpReq.send(null); // 发送请求
}
// 处理返回信息函数
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判断对象状态
if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息
document.getElementById("product").innerText = xmlHttp.responseText
+ "dsadsa";
//document.getElementById("count").innerHTML = xmlHttp.responseText;
setInterval("sendRequest()", 1000)
} else { //页面不正常
window.alert("您所请求的页面有异常。");
}
}
}
window.onunload = function() {

}
window.onbeforeunload = function() {

}
//function DisplayHot() {
//var name = XMLHttpReq.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
//var count = XMLHttpReq.responseXML.getElementsByTagName("count")[0].firstChild.nodeValue;
//document.getElementById("product").innerHTML = name;
//document.getElementById("count").innerHTML = count;
//}
</script>
<body onload=sendRequest()>
<table align="center" style="BORDER-COLLAPSE: collapse"
borderColor=#111111 cellSpacing=0 cellPadding=0 width=500
bgColor=#f5efe7 border=0>
<TR>
<TD align=center bgColor=#dbc2b0 height=19 colspan="2"><B>服务器当前信息</B>
</TD>
</TR>
<tr>
<td height="20">当前连接数为:<%=Bean0.getConnNum()%></td>
<td height="20"></td>
</tr>
<tr>
<td height="20">当前CPU占用率为:<%=Bean0.getCpuInfo()%></td>
<td height="20"></td>
</tr>
<tr>
<td height="20">上传流量:</td>
<td height="20" id="product"></td>
</tr>
<tr>
<td height="20">下载流量:</td>
<td height="20" id="count"></td>
</tr>
</table>
</body>
</html>



<%@page import="com.network.NetWorkThread"%>
<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<jsp:useBean id="Bean0" scope="application"
class="com.codyy.test.GetInfo" />
<jsp:useBean id="Bean1" scope="application"
class="com.network.NetWorkThread" />
<%
//out.println("当前连接数为:" + Bean0.getConnNum() + "");
//out.println("<br>");
//out.println("当前CPU占用率为::" + Bean0.getCpuInfo() + "");
//out.println("<br>");
//out.println("当前流量接收为:" + Bean0.getLiuLiangRececied() + "");
//out.println("当前流量发送为:" + Bean0.getLiuLiangSend() + "");
//out.print(Bean1.getRececied());
out.print("asdasdjasj");
System.out.println("ssssssss");
//out.clearBuffer();
//if (!request.getParameter("gogo").equals("love")) {
// System.out.println("aaaaaaaaaaaa");
// out.print(Bean1.getRececied());
//}
//out.flush();
%>
</html>
小裴同学 2011-12-29
  • 打赏
  • 举报
回复
谢谢大家帮忙问题解决了。用ajax实在不知道怎么写代码。最后用的jquery。。
谁在哀伤望月 2011-12-28
  • 打赏
  • 举报
回复
用推送,数据从后台源源不断的推送到客户端的网页去
五哥 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lockedstar 的回复:]
1.使用js,运用div渲染来实现,如下:
function timer0(){
x2.value = "刷新倒计时";
x2.value += c--;
if(c >= 0){timeId1 = window.setTimeout(timer0,1000);}else{location.reload();}
}

2.jsp的话,只能刷新这个页面了,不太好,不推荐,我就列出三种了。
2.1<%esponse.setHeader("refresh","1");%>
每一秒刷新一次
2.2.使用javascript:
<script>
setTimeout("self.location.reload();",1000);
<script>
2.3<meta http-equiv="refresh" content="20">
其中20指每隔20秒刷新一次页面[/Quote]

+1
东风无眠 2011-12-28
  • 打赏
  • 举报
回复
1.使用js,运用div渲染来实现,如下:
function timer0(){
x2.value = "刷新倒计时";
x2.value += c--;
if(c >= 0){timeId1 = window.setTimeout(timer0,1000);}else{location.reload();}
}

2.jsp的话,只能刷新这个页面了,不太好,不推荐,我就列出三种了。
2.1<%esponse.setHeader("refresh","1");%>
每一秒刷新一次
2.2.使用javascript:
<script>
setTimeout("self.location.reload();",1000);
<script>
2.3<meta http-equiv="refresh" content="20">
其中20指每隔20秒刷新一次页面.
达布留西 2011-12-28
  • 打赏
  • 举报
回复
<script>
function show(){
var date = new Date(); //日期对象
var now = "";
now = date.getFullYear()+"年"; //读英文就行了
now = now + (date.getMonth()+1)+"月"; //取月的时候取的是当前月-1如果想取当前月+1就可以了
now = now + date.getDate()+"日";
now = now + date.getHours()+"时";
now = now + date.getMinutes()+"分";
now = now + date.getSeconds()+"秒";
document.getElementById("nowDiv").innerHTML = now; //div的html是now这个字符串
setTimeout("show()",1000); //设置过1000毫秒就是1秒,调用show方法
}
</script>
<body onload="show()"> <!-- 网页加载时调用一次 以后就自动调用了-->
<div id="nowDiv"></div>
</body>
直接复制就可以看效果了
达布留西 2011-12-28
  • 打赏
  • 举报
回复
<script>
function show(){
var date = new Date(); //日期对象
var now = "";
now = date.getFullYear()+"年"; //读英文就行了
now = now + (date.getMonth()+1)+"月"; //取月的时候取的是当前月-1如果想取当前月+1就可以了
now = now + date.getDate()+"日";
now = now + date.getHours()+"时";
now = now + date.getMinutes()+"分";
now = now + date.getSeconds()+"秒";
document.getElementById("nowDiv").innerHTML = now; //div的html是now这个字符串
setTimeout("show()",1000); //设置过1000毫秒就是1秒,调用show方法
}
</script>
<body onload="show()"> <!-- 网页加载时调用一次 以后就自动调用了-->
<div id="nowDiv"></div>
</body>
直接复制就可以看效果了
小裴同学 2011-12-28
  • 打赏
  • 举报
回复
嗯。我知道这个,关键是怎么操作。。。
达布留西 2011-12-28
  • 打赏
  • 举报
回复
<script>
function show(){
var date = new Date(); //日期对象
var now = "";
now = date.getFullYear()+"年"; //读英文就行了
now = now + (date.getMonth()+1)+"月"; //取月的时候取的是当前月-1如果想取当前月+1就可以了
now = now + date.getDate()+"日";
now = now + date.getHours()+"时";
now = now + date.getMinutes()+"分";
now = now + date.getSeconds()+"秒";
document.getElementById("nowDiv").innerHTML = now; //div的html是now这个字符串
setTimeout("show()",1000); //设置过1000毫秒就是1秒,调用show方法
}
</script>
<body onload="show()"> <!-- 网页加载时调用一次 以后就自动调用了-->
<div id="nowDiv"></div>
</body>
直接复制就可以看效果了
  • 打赏
  • 举报
回复
AJAX技术,1秒钟去后台读一次这个值,然后将页面上的值替换掉
小裴同学 2011-12-28
  • 打赏
  • 举报
回复
<%@page import="com.network.NetWorkThread"%>
<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录界面</title>
</head>
<jsp:useBean id="Bean0" scope="application"
class="com.codyy.test.GetInfo" />
<jsp:useBean id="Bean1" scope="application"
class="com.network.NetWorkThread" />
<%
new Thread(Bean1).start();
out.println("当前连接数为:" + Bean0.getConnNum() + "");
out.println("<br>");
out.println("当前CPU占用率为::" + Bean0.getCpuInfo() + "");
out.println("<br>");
//out.println("当前流量接收为:" + Bean0.getLiuLiangRececied() + "");
//out.println("<br>");
//out.println("当前流量发送为:" + Bean0.getLiuLiangSend() + "");
for(int i=0;i<100;i++){
Thread.sleep(1000);
System.out.println(Bean1.getRececied());

}
%>
</html>
Bean1.getRececied()取得的是一个String,每个一秒去取都都不一样。
你们看我这个代码打印出来的就是一个一直变化的值,请问怎么显示在界面上啊。。
现在我不知道怎么在界面上显示这个变化的数据。。求帮忙
wjsq0705 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cai5 的回复:]

引用 6 楼 lockedstar 的回复:
1.使用js,运用div渲染来实现,如下:
function timer0(){
x2.value = "刷新倒计时";
x2.value += c--;
if(c >= 0){timeId1 = window.setTimeout(timer0,1000);}else{location.reload();}
}

2.jsp的话,只……
[/Quote]



没试验过,不过明显你的代码有问题,你show方法里面一直调用自己,可实际里面什么也没做,你可以把取数据的事交给show方法去做,再把数据显示下面。
LFDream 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 peijiangping1989 的回复:]
嗯,我不是要日期呢,你们的日期代码都是在js里面。我想js里面可以获取我的数据吗
[/Quote]

不明白lZ的意思
小裴同学 2011-12-28
  • 打赏
  • 举报
回复
嗯,我不是要日期呢,你们的日期代码都是在js里面。我想js里面可以获取我的数据吗
LFDream 2011-12-28
  • 打赏
  • 举报
回复
这个很简单,我直接给你列子吧、你拷贝出去直接去运行下、看看是否是你想要的效果、。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>时期时间显示</title>
</head>
<body>
<div id="jnkc"></div>
<script>
  setInterval("jnkc.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);
</script>
</body>
</html>


[Quote=引用楼主 peijiangping1989 的回复:]
比如说,界面上一直在显示一个时间。动态的走。1秒变化一次。这样的代码怎么写哇。。
嗯,最好有代码,谢谢咯。
我这里是有一个String 这个String是当前的流量。比如180kb/s。我想一秒刷新一次这个数据。我在后台可以得到,就是想怎么在jsp上面显示。
[/Quote]
小裴同学 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 lei_00 的回复:]

用jquery一切都变得简单。
[/Quote]
我先百度下,你能说下怎么用嘛
IAMLEI00 2011-12-28
  • 打赏
  • 举报
回复
用jquery一切都变得简单。
小裴同学 2011-12-28
  • 打赏
  • 举报
回复
有没有帮忙的哇,感激不尽,我现在也正在看ajax的教程。不知道怎么办哇。
小裴同学 2011-12-28
  • 打赏
  • 举报
回复
<%@page import="com.network.NetWorkThread"%>
<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录界面</title>
</head>
<jsp:useBean id="Bean0" scope="application"
class="com.codyy.test.GetInfo" />
<jsp:useBean id="Bean1" scope="application"
class="com.network.NetWorkThread" />
<%
new Thread(Bean1).start();
out.println("当前连接数为:" + Bean0.getConnNum() + "");
out.println("<br>");
out.println("当前CPU占用率为::" + Bean0.getCpuInfo() + "");
out.println("<br>");
//out.println("当前流量接收为:" + Bean0.getLiuLiangRececied() + "");
//out.println("<br>");
//out.println("当前流量发送为:" + Bean0.getLiuLiangSend() + "");
for(int i=0;i<100;i++){
Thread.sleep(1000);
System.out.println(Bean1.getRececied());

}
%>
</html>
你们看我这个代码打印出来的就是一个一直变化的值,请问怎么显示在界面上啊。。
加载更多回复(5)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧