本文转自:http://blog.163.com/girl_lihuiyue@126/blog/static/180696212011816111617840/
dwr推技术(服务器向jsp页面推送弹出框--bs)
应用背景:(bs/cs分别都有说明)
我们的项目是一个服刑人员监控系统,定位犯人位置,如果进入不允许进入的区域就需要向前台页面推出一个报警弹出框,列出报警信息
好处:(bs)
不需要页面使用ajax每秒钟重写报警弹出框的内容,减少浏览器开销,防止浏览器崩溃(之前我使用的就是页面重写,结果运行半小时左右浏览器就崩溃,所以后来改用了推技术实现,操作放到后台,减少浏览器压力)
bs向jsp页面推送弹出框代码如下:
package com.test.dwr.bean;
public class Message {
private String msgId;
private String context;
public Message() {
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
package com.test.dwr.servlet;
import java.util.Collection;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
import com.test.dwr.bean.Message;
public class Test{
@SuppressWarnings("unchecked")
public void sendMsg(Message u) {
WebContext wctx = WebContextFactory.get(); // 这里是获取WebContext上下文
String clientPage = "/dwr/index.jsp";// 推到哪个页面
// 要求的必须方式
Collection sessions = wctx.getScriptSessionsByPage(clientPage); // 在一个page中可能存在多个ScriptSessions,
Util utilAll = new Util(sessions); // Util 是DWR 在Server端模拟Brower端
String ss = u.getContext();
utilAll.setValue("aa", ss);
utilAll.setStyle("bb", "display", "inline");
utilAll.setValue("bb", "<p>"+u.getContext()+"</p>", false);
//ScriptBuffer sb = new ScriptBuffer();
utilAll.addFunctionCall("divShow", u.getContext());
}
}
页面index.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<head>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/dwr/engine.js'></script>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/dwr/util.js'></script>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/js/jquery-1.5.1.js'></script>
<script type="text/javascript">
function clo ()
{
var obj = $("#msgDIV");
obj.hide("slow");
}
function divShow(message) {
$("#content").append("<div id='msgDIV' style='position: absolute;right: 2px;bottom: 20px;display:none;padding: 2px;width:200px;height: 200px;border: 1px solid black;'>");
$("#msgDIV").append("<div id='close' style='cursor:hand' align='right' οnclick='clo()'>close</div>");
$("#msgDIV").append("<div id='msg' align='center'><font color='red'></font></div>");
$("#msgDIV").append("<div align='right'><font color='bule'><a href='http://blog.163.com/girl_lihuiyue@126/blog/#'>click here to more</a></font></div>");
$("#content").append("</div>");
$("#msg").html("<p>"+message+"</p>");
$("#msgDIV").show("slow");
}
</script>
</head>
<body οnlοad="dwr.engine.setActiveReverseAjax(true)">
<div id="content">
<input type="text" name="aa" id="aa" />
<div id="bb" style="display: none; width: 300px; height: 300px">
</div>
</div>
</body>
页面MyJsp.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<head>
<script type="text/javascript" src="http://blog.163.com/girl_lihuiyue@126/blog/<%=request.getContextPath() %>/js/jquery-1.5.1.js"></script>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/dwr/engine.js'></script>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/dwr/util.js'></script>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/dwr/interface/StocksPusher.js'></script>
<script type="text/javascript">
function test() {
var msg = null;
msg = {msgId: '1', context: $("#msgContext").val()};
StocksPusher.sendMsg(msg);
}
</script>
</head>
<body>
<input type="text" name="msgId" id="msgId" /> <br />
context : <input type="text" name="msgContext" id="msgContext" />
<input type="button" value="Send" οnclick="test()" />
</body>
</html>
查看例子说明:
同时打开MyJsp.jsp和index.jsp
在MyJsp.jsp里面输入内容点击发送,index.jsp页面就直接接收到了推送的内容
这里只是简单的介绍,除了页面和后台的代码外,还有js文件,xml文件,jar包等等。
好处:(bs)
不需要页面使用ajax每秒钟重写报警弹出框的内容,减少浏览器开销,防止浏览器崩溃(之前我使用的就是页面重写,结果运行半小时左右浏览器就崩溃,所以后来改用了推技术实现,操作放到后台,减少浏览器压力)
bs向jsp页面推送弹出框代码如下:
package com.test.dwr.bean;
public class Message {
private String msgId;
private String context;
public Message() {
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
package com.test.dwr.servlet;
import java.util.Collection;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
import com.test.dwr.bean.Message;
public class Test{
@SuppressWarnings("unchecked")
public void sendMsg(Message u) {
WebContext wctx = WebContextFactory.get(); // 这里是获取WebContext上下文
String clientPage = "/dwr/index.jsp";// 推到哪个页面
// 要求的必须方式
Collection sessions = wctx.getScriptSessionsByPage(clientPage); // 在一个page中可能存在多个ScriptSessions,
Util utilAll = new Util(sessions); // Util 是DWR 在Server端模拟Brower端
String ss = u.getContext();
utilAll.setValue("aa", ss);
utilAll.setStyle("bb", "display", "inline");
utilAll.setValue("bb", "<p>"+u.getContext()+"</p>", false);
//ScriptBuffer sb = new ScriptBuffer();
utilAll.addFunctionCall("divShow", u.getContext());
}
}
页面index.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<head>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/dwr/engine.js'></script>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/dwr/util.js'></script>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/js/jquery-1.5.1.js'></script>
<script type="text/javascript">
function clo ()
{
var obj = $("#msgDIV");
obj.hide("slow");
}
function divShow(message) {
$("#content").append("<div id='msgDIV' style='position: absolute;right: 2px;bottom: 20px;display:none;padding: 2px;width:200px;height: 200px;border: 1px solid black;'>");
$("#msgDIV").append("<div id='close' style='cursor:hand' align='right' οnclick='clo()'>close</div>");
$("#msgDIV").append("<div id='msg' align='center'><font color='red'></font></div>");
$("#msgDIV").append("<div align='right'><font color='bule'><a href='http://blog.163.com/girl_lihuiyue@126/blog/#'>click here to more</a></font></div>");
$("#content").append("</div>");
$("#msg").html("<p>"+message+"</p>");
$("#msgDIV").show("slow");
}
</script>
</head>
<body οnlοad="dwr.engine.setActiveReverseAjax(true)">
<div id="content">
<input type="text" name="aa" id="aa" />
<div id="bb" style="display: none; width: 300px; height: 300px">
</div>
</div>
</body>
页面MyJsp.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<head>
<script type="text/javascript" src="http://blog.163.com/girl_lihuiyue@126/blog/<%=request.getContextPath() %>/js/jquery-1.5.1.js"></script>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/dwr/engine.js'></script>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/dwr/util.js'></script>
<script type='text/javascript' src='http://blog.163.com/girl_lihuiyue@126/blog/dwr/interface/StocksPusher.js'></script>
<script type="text/javascript">
function test() {
var msg = null;
msg = {msgId: '1', context: $("#msgContext").val()};
StocksPusher.sendMsg(msg);
}
</script>
</head>
<body>
<input type="text" name="msgId" id="msgId" /> <br />
context : <input type="text" name="msgContext" id="msgContext" />
<input type="button" value="Send" οnclick="test()" />
</body>
</html>
查看例子说明:
同时打开MyJsp.jsp和index.jsp
在MyJsp.jsp里面输入内容点击发送,index.jsp页面就直接接收到了推送的内容
这里只是简单的介绍,除了页面和后台的代码外,还有js文件,xml文件,jar包等等。