4 个解决方案
#1
可以这么做:在你的系统主页上(就是用户打的的页面)放个
<iframe src="xxx.jsp" width="0" height="0" marginwidth="0" marginheight="0" frameborder="0" scrolling="No" noresize="noresize"></iframe>
这个小窗口的长度和宽度都是0就是在页面上是不显示的,那么xxx.jsp的代码就是判断是否有管理员的信息(管理员信息存入数据库)也就是刷新看数据库里是否有新记录,如果有新记录就以弹出小窗口显示信息,比如10秒20秒刷新一次,这样就完成你上述的功能了
不过我们一般不用直接弹出窗口的方式来做,一般我们把iframe 的长和宽设小一点比如100,那么就把这个小窗口放在左下角或右上角,当有新消息时就用鲜艳的字来提醒用户有新消息,当初我还在这个小窗口做了一个小秘书的FLASH(类似WORD里的小秘书)当有新消息就用动画和声音(你有新短消息,请注意查收)来提醒用户。
<iframe src="xxx.jsp" width="0" height="0" marginwidth="0" marginheight="0" frameborder="0" scrolling="No" noresize="noresize"></iframe>
这个小窗口的长度和宽度都是0就是在页面上是不显示的,那么xxx.jsp的代码就是判断是否有管理员的信息(管理员信息存入数据库)也就是刷新看数据库里是否有新记录,如果有新记录就以弹出小窗口显示信息,比如10秒20秒刷新一次,这样就完成你上述的功能了
不过我们一般不用直接弹出窗口的方式来做,一般我们把iframe 的长和宽设小一点比如100,那么就把这个小窗口放在左下角或右上角,当有新消息时就用鲜艳的字来提醒用户有新消息,当初我还在这个小窗口做了一个小秘书的FLASH(类似WORD里的小秘书)当有新消息就用动画和声音(你有新短消息,请注意查收)来提醒用户。
#2
谢谢楼上的, 可是按你的方法, 还是在不停的刷新xxx.jsp来查看有无消息呀, 现在是要求在不刷新的情况下, 如果有消息, 就显示, 我知道servlet里有种叫推送(push)的技术, 好象可以达到这种效果, 但不知道怎么用, 希望知道的大侠们赐教!
#3
学习一下
#4
Ajax异步请求,servlet产生随机数据,Html页面无刷新显示。
运行环境:jdk1.5+tomcat 5.5
一、ajaxServlet.java
package com;
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;
import java.util.Random;
public class ajaxServlet extends HttpServlet{
private static final String CONTENT_TYPE="text/xml; charset=gb2312";
public void init() throws ServletException{}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType(CONTENT_TYPE);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires",0);
PrintWriter out=response.getWriter();
String action=request.getParameter("action");
if(("send").equals(action)){
Random random = new Random();
String AA=Integer.toString(Math.abs(random.nextInt()));
String BB=Integer.toString(Math.abs(random.nextInt()));
String CC=Integer.toString(Math.abs(random.nextInt()));
String DD=Integer.toString(Math.abs(random.nextInt()));
StringBuffer sb=new StringBuffer();
sb.append(AA); sb.append("@@@");
sb.append(BB); sb.append("@@@");
sb.append(CC); sb.append("@@@");
sb.append(DD);
out.write(sb.toString());
out.close();
} }}
二、testajax.html
<html>
<head>
<title>AJAX.html</title>
</head>
<script type="text/javascript">
function getResult(){
var url = "/Tajax/AajaxServlet?action=send";
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url, true);
req.onreadystatechange = complete;
req.send(null);
}
}
/*分析返回的文本文档*/
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
// alert(req.responseText);
var strResult = unescape(req.responseText);
var arrResult = strResult.split("@@@");
RemoveRow(); //删除以前的数据.
num1 = arrResult[0]; //字段num1的值
num2 = arrResult[1]; //字段num2的值
num3 = arrResult[2];
num4 = arrResult[3];
row1= tb.insertRow();
cell1 = row1.insertCell();
cell1.innerText = num1;
cell2 = row1.insertCell();
cell2.innerText = num2;
row2= tb.insertRow();
cell3 = row2.insertCell();
cell3.innerText = num3;
cell4 = row2.insertCell();
cell4.innerText = num4;
} else {
alert('There was a problem with the request.');
}
}
}
function RemoveRow(){//保留第一行表头,其余数据均删除.
var iRows = tb.rows.length;
for(var i=0;i< iRows-1;i++){
tb.deleteRow(1);
}
}
function MyShow(){//2秒自动刷新一次,2秒取得一次数据.
timer = window.setInterval("getResult()",2000);
}
</script>
<body onload="MyShow()">
<table width="47%" height="23" border="0" cellpadding="1" cellspacing="0" id="tb">
<tr><td>代码</td>
<td>价格</td>
</tr>
</table>
<form>
<p>输入股票代码:
<input type="text" size="14" name="phone" id="phone"/>
</p>
<p>你交易的数量:</p>
<p><textarea name="order" rows="6" cols="50" id="order"></textarea></p>
<p><input type="submit" value="提交" id="submit" /></p>
</form>
</body>
三、web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>AajaxServlet</servlet-name>
<servlet-class>com.ajaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AajaxServlet</servlet-name>
<url-pattern>/AajaxServlet</url-pattern>
</servlet-mapping>
</web-app>
运行环境:jdk1.5+tomcat 5.5
一、ajaxServlet.java
package com;
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;
import java.util.Random;
public class ajaxServlet extends HttpServlet{
private static final String CONTENT_TYPE="text/xml; charset=gb2312";
public void init() throws ServletException{}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType(CONTENT_TYPE);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires",0);
PrintWriter out=response.getWriter();
String action=request.getParameter("action");
if(("send").equals(action)){
Random random = new Random();
String AA=Integer.toString(Math.abs(random.nextInt()));
String BB=Integer.toString(Math.abs(random.nextInt()));
String CC=Integer.toString(Math.abs(random.nextInt()));
String DD=Integer.toString(Math.abs(random.nextInt()));
StringBuffer sb=new StringBuffer();
sb.append(AA); sb.append("@@@");
sb.append(BB); sb.append("@@@");
sb.append(CC); sb.append("@@@");
sb.append(DD);
out.write(sb.toString());
out.close();
} }}
二、testajax.html
<html>
<head>
<title>AJAX.html</title>
</head>
<script type="text/javascript">
function getResult(){
var url = "/Tajax/AajaxServlet?action=send";
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url, true);
req.onreadystatechange = complete;
req.send(null);
}
}
/*分析返回的文本文档*/
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
// alert(req.responseText);
var strResult = unescape(req.responseText);
var arrResult = strResult.split("@@@");
RemoveRow(); //删除以前的数据.
num1 = arrResult[0]; //字段num1的值
num2 = arrResult[1]; //字段num2的值
num3 = arrResult[2];
num4 = arrResult[3];
row1= tb.insertRow();
cell1 = row1.insertCell();
cell1.innerText = num1;
cell2 = row1.insertCell();
cell2.innerText = num2;
row2= tb.insertRow();
cell3 = row2.insertCell();
cell3.innerText = num3;
cell4 = row2.insertCell();
cell4.innerText = num4;
} else {
alert('There was a problem with the request.');
}
}
}
function RemoveRow(){//保留第一行表头,其余数据均删除.
var iRows = tb.rows.length;
for(var i=0;i< iRows-1;i++){
tb.deleteRow(1);
}
}
function MyShow(){//2秒自动刷新一次,2秒取得一次数据.
timer = window.setInterval("getResult()",2000);
}
</script>
<body onload="MyShow()">
<table width="47%" height="23" border="0" cellpadding="1" cellspacing="0" id="tb">
<tr><td>代码</td>
<td>价格</td>
</tr>
</table>
<form>
<p>输入股票代码:
<input type="text" size="14" name="phone" id="phone"/>
</p>
<p>你交易的数量:</p>
<p><textarea name="order" rows="6" cols="50" id="order"></textarea></p>
<p><input type="submit" value="提交" id="submit" /></p>
</form>
</body>
三、web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>AajaxServlet</servlet-name>
<servlet-class>com.ajaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AajaxServlet</servlet-name>
<url-pattern>/AajaxServlet</url-pattern>
</servlet-mapping>
</web-app>
#1
可以这么做:在你的系统主页上(就是用户打的的页面)放个
<iframe src="xxx.jsp" width="0" height="0" marginwidth="0" marginheight="0" frameborder="0" scrolling="No" noresize="noresize"></iframe>
这个小窗口的长度和宽度都是0就是在页面上是不显示的,那么xxx.jsp的代码就是判断是否有管理员的信息(管理员信息存入数据库)也就是刷新看数据库里是否有新记录,如果有新记录就以弹出小窗口显示信息,比如10秒20秒刷新一次,这样就完成你上述的功能了
不过我们一般不用直接弹出窗口的方式来做,一般我们把iframe 的长和宽设小一点比如100,那么就把这个小窗口放在左下角或右上角,当有新消息时就用鲜艳的字来提醒用户有新消息,当初我还在这个小窗口做了一个小秘书的FLASH(类似WORD里的小秘书)当有新消息就用动画和声音(你有新短消息,请注意查收)来提醒用户。
<iframe src="xxx.jsp" width="0" height="0" marginwidth="0" marginheight="0" frameborder="0" scrolling="No" noresize="noresize"></iframe>
这个小窗口的长度和宽度都是0就是在页面上是不显示的,那么xxx.jsp的代码就是判断是否有管理员的信息(管理员信息存入数据库)也就是刷新看数据库里是否有新记录,如果有新记录就以弹出小窗口显示信息,比如10秒20秒刷新一次,这样就完成你上述的功能了
不过我们一般不用直接弹出窗口的方式来做,一般我们把iframe 的长和宽设小一点比如100,那么就把这个小窗口放在左下角或右上角,当有新消息时就用鲜艳的字来提醒用户有新消息,当初我还在这个小窗口做了一个小秘书的FLASH(类似WORD里的小秘书)当有新消息就用动画和声音(你有新短消息,请注意查收)来提醒用户。
#2
谢谢楼上的, 可是按你的方法, 还是在不停的刷新xxx.jsp来查看有无消息呀, 现在是要求在不刷新的情况下, 如果有消息, 就显示, 我知道servlet里有种叫推送(push)的技术, 好象可以达到这种效果, 但不知道怎么用, 希望知道的大侠们赐教!
#3
学习一下
#4
Ajax异步请求,servlet产生随机数据,Html页面无刷新显示。
运行环境:jdk1.5+tomcat 5.5
一、ajaxServlet.java
package com;
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;
import java.util.Random;
public class ajaxServlet extends HttpServlet{
private static final String CONTENT_TYPE="text/xml; charset=gb2312";
public void init() throws ServletException{}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType(CONTENT_TYPE);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires",0);
PrintWriter out=response.getWriter();
String action=request.getParameter("action");
if(("send").equals(action)){
Random random = new Random();
String AA=Integer.toString(Math.abs(random.nextInt()));
String BB=Integer.toString(Math.abs(random.nextInt()));
String CC=Integer.toString(Math.abs(random.nextInt()));
String DD=Integer.toString(Math.abs(random.nextInt()));
StringBuffer sb=new StringBuffer();
sb.append(AA); sb.append("@@@");
sb.append(BB); sb.append("@@@");
sb.append(CC); sb.append("@@@");
sb.append(DD);
out.write(sb.toString());
out.close();
} }}
二、testajax.html
<html>
<head>
<title>AJAX.html</title>
</head>
<script type="text/javascript">
function getResult(){
var url = "/Tajax/AajaxServlet?action=send";
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url, true);
req.onreadystatechange = complete;
req.send(null);
}
}
/*分析返回的文本文档*/
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
// alert(req.responseText);
var strResult = unescape(req.responseText);
var arrResult = strResult.split("@@@");
RemoveRow(); //删除以前的数据.
num1 = arrResult[0]; //字段num1的值
num2 = arrResult[1]; //字段num2的值
num3 = arrResult[2];
num4 = arrResult[3];
row1= tb.insertRow();
cell1 = row1.insertCell();
cell1.innerText = num1;
cell2 = row1.insertCell();
cell2.innerText = num2;
row2= tb.insertRow();
cell3 = row2.insertCell();
cell3.innerText = num3;
cell4 = row2.insertCell();
cell4.innerText = num4;
} else {
alert('There was a problem with the request.');
}
}
}
function RemoveRow(){//保留第一行表头,其余数据均删除.
var iRows = tb.rows.length;
for(var i=0;i< iRows-1;i++){
tb.deleteRow(1);
}
}
function MyShow(){//2秒自动刷新一次,2秒取得一次数据.
timer = window.setInterval("getResult()",2000);
}
</script>
<body onload="MyShow()">
<table width="47%" height="23" border="0" cellpadding="1" cellspacing="0" id="tb">
<tr><td>代码</td>
<td>价格</td>
</tr>
</table>
<form>
<p>输入股票代码:
<input type="text" size="14" name="phone" id="phone"/>
</p>
<p>你交易的数量:</p>
<p><textarea name="order" rows="6" cols="50" id="order"></textarea></p>
<p><input type="submit" value="提交" id="submit" /></p>
</form>
</body>
三、web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>AajaxServlet</servlet-name>
<servlet-class>com.ajaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AajaxServlet</servlet-name>
<url-pattern>/AajaxServlet</url-pattern>
</servlet-mapping>
</web-app>
运行环境:jdk1.5+tomcat 5.5
一、ajaxServlet.java
package com;
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;
import java.util.Random;
public class ajaxServlet extends HttpServlet{
private static final String CONTENT_TYPE="text/xml; charset=gb2312";
public void init() throws ServletException{}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType(CONTENT_TYPE);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires",0);
PrintWriter out=response.getWriter();
String action=request.getParameter("action");
if(("send").equals(action)){
Random random = new Random();
String AA=Integer.toString(Math.abs(random.nextInt()));
String BB=Integer.toString(Math.abs(random.nextInt()));
String CC=Integer.toString(Math.abs(random.nextInt()));
String DD=Integer.toString(Math.abs(random.nextInt()));
StringBuffer sb=new StringBuffer();
sb.append(AA); sb.append("@@@");
sb.append(BB); sb.append("@@@");
sb.append(CC); sb.append("@@@");
sb.append(DD);
out.write(sb.toString());
out.close();
} }}
二、testajax.html
<html>
<head>
<title>AJAX.html</title>
</head>
<script type="text/javascript">
function getResult(){
var url = "/Tajax/AajaxServlet?action=send";
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url, true);
req.onreadystatechange = complete;
req.send(null);
}
}
/*分析返回的文本文档*/
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
// alert(req.responseText);
var strResult = unescape(req.responseText);
var arrResult = strResult.split("@@@");
RemoveRow(); //删除以前的数据.
num1 = arrResult[0]; //字段num1的值
num2 = arrResult[1]; //字段num2的值
num3 = arrResult[2];
num4 = arrResult[3];
row1= tb.insertRow();
cell1 = row1.insertCell();
cell1.innerText = num1;
cell2 = row1.insertCell();
cell2.innerText = num2;
row2= tb.insertRow();
cell3 = row2.insertCell();
cell3.innerText = num3;
cell4 = row2.insertCell();
cell4.innerText = num4;
} else {
alert('There was a problem with the request.');
}
}
}
function RemoveRow(){//保留第一行表头,其余数据均删除.
var iRows = tb.rows.length;
for(var i=0;i< iRows-1;i++){
tb.deleteRow(1);
}
}
function MyShow(){//2秒自动刷新一次,2秒取得一次数据.
timer = window.setInterval("getResult()",2000);
}
</script>
<body onload="MyShow()">
<table width="47%" height="23" border="0" cellpadding="1" cellspacing="0" id="tb">
<tr><td>代码</td>
<td>价格</td>
</tr>
</table>
<form>
<p>输入股票代码:
<input type="text" size="14" name="phone" id="phone"/>
</p>
<p>你交易的数量:</p>
<p><textarea name="order" rows="6" cols="50" id="order"></textarea></p>
<p><input type="submit" value="提交" id="submit" /></p>
</form>
</body>
三、web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>AajaxServlet</servlet-name>
<servlet-class>com.ajaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AajaxServlet</servlet-name>
<url-pattern>/AajaxServlet</url-pattern>
</servlet-mapping>
</web-app>