例子:
web.xml文件配置:
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- DWR默认采用piggyback方式 -->
<!-- 使用polling和comet的方式 -->
<init-param>
<param-name>pollAndCometEnabled</param-name>
<param-value>true</param-value>
</init-param>
<!-- comet方式 -->
<!--
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
-->
<!-- polling方式:在comet方式的基础之上,再配置以下参数 -->
<!--
<init-param>
<param-name>org.directwebremoting.extend.ServerLoadMonitor</param-name>
<param-value>org.directwebremoting.impl.PollingServerLoadMonitor</param-value>
</init-param>
-->
<!-- 毫秒数。页面默认的请求间隔时间是5秒 -->
<!--
<init-param>
<param-name>disconnectedTime</param-name>
<param-value>60000</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
java方法:
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.locks.ReentrantLock;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.proxy.dwr.Util;
import uk.ltd.getahead.dwr.WebContext;
import uk.ltd.getahead.dwr.WebContextFactory;
private static LinkedList<Message> messages = new LinkedList<Message>();
private static ReentrantLock lock = new ReentrantLock(); //JDK5锁
public void addMessage(String text){
System.out.println("---------");
try{
lock.lock();
if(text!=null && text.trim().length()>0){
messages.addFirst(new Message(text));
if(messages.size()>10){
messages.removeLast();
}
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
lock.unlock();
}
System.out.println("----1-----");
//获得DWR上下文
WebContext webContext = WebContextFactory.get();
System.out.println("----2-----");
//获取当前页面URL,比如/ext3/test_tag.jsp
String currentPage = webContext.getCurrentPage();
System.out.println("----3-----");
//当前脚本sessin
ScriptSession scriptSession = webContext.getScriptSession();
System.out.println("-----4----");
//设置页面控件的值
Util util = new Util(scriptSession);
util.setValue("text", ""); //这里是清空页面输入框的值
System.out.println("----5-----");
//设置脚本sessin的属性值
scriptSession.setAttribute("uid", "cjm");
System.out.println("------6---");
//获取脚本session的属性值
for(Iterator it=scriptSession.getAttributeNames();it.hasNext();){
String attrName = (String)it.next();
System.out.println(attrName + "=" + scriptSession.getAttribute(attrName));
}
System.out.println("----7-----");
//获取所有浏览当前页面的脚本session
Collection<ScriptSession> sessions = webContext.getScriptSessionsByPage(currentPage);
System.out.println("----8-----");
Util utilAll = new Util(sessions);
System.out.println("-----9----");
//执行客户端脚本
ScriptBuffer script = new ScriptBuffer();
script.appendScript("clientFunction(")
.appendData(scriptSession.getAttribute("uid"))
.appendScript(");");
System.out.println("----10-----");
for(ScriptSession session: sessions){
session.addScript(script);
}
System.out.println("----11-----");
//更新这些脚本session的一些元素
utilAll.removeAllOptions("messages");
utilAll.addOptions("messages", messages, "id", "text");
}
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.zzst.application.meeting.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'ajax0729.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type='text/javascript' src='<%=MeetingConfig.CONTENT_PATH%>/dwr/engine.js'> </script>
<script type='text/javascript' src='<%=MeetingConfig.CONTENT_PATH%>/dwr/util.js'> </script>
<script type='text/javascript' src='<%=MeetingConfig.CONTENT_PATH %>/dwr/interface/DwrMethod.js'></script>
</head>
<body onload="dwr.engine.setActiveReverseAjax(true);sendMessage();">
<p>输入信息: <input id="text" onkeypress="dwr.util.onReturn(event, sendMessage)" />
<input type="button" value="Send" onclick="sendMessage()" /></p>
<script type="text/javascript">
function sendMessage() {
DwrMethod.addMessage(dwr.util.getValue("text"));
}
function clientFunction(returnStr){
//document.all.view.value="已经广播给所有看到这个页面的用户了"+returnStr
}
</script>
<input id="view">
<hr/>
<select id="messages"></select>
</body>
</html>
分享到:
相关推荐
包含了dwr推送技术的详细介绍 和 一个基于dwr推送技术的聊天实例
dwr的反转ajax,实现服务器推送数据。
DWR服务器推送技术(实时),能实时查看注册的用户和收发的消息
实现 Dwr 服务器推送实现 Dwr 服务器推送实现 Dwr 服务器推送实现 Dwr 服务器推送实现 Dwr 服务器推送
DWR+ spring 实现推送技术
该工程是dwr推送以及javascript动态调用java后台代码的实例
dwr3 推送技术,可以指定用户发送登录者。但是dwr里会有许多alert的弹出框。
使用dwr+spring实现消息推送 。
Dwr推送技术实现BS即时通讯,项目源代码,功能实现
基于dwr推送技术的聊天室,下载后解压,可直接运行
DWR消息推送简单实例。maven工程。
本实例是在一些网络资料的基础上整合出来的一个:以后台向前台页面推送消息的一个完整实例工程,采用了spring、dwr反转,实现的是后台向前台不断推送消息,并管理相关用户退出;可以用在如定时任务的桌面消息提醒之...
该文档介绍了dwr实现消息推送的具体步骤,希望对大家有所帮助,若有不明白的地方,可以给我发消息,小弟能解答的一定解答。
用DWR推送数据,采用comet方式
dwr消息推送功能。可以实现实时消息推送
springMVC+dwr技术实现消息推送实例,简单实例演示如何用dwr实现后台向前台主动推送消息
里面有详细的注释说明,主要实现DWR推送、线程监听、事件监听、主动推送。。写得不好,请见谅。。然后有个特别需要注意的,部署好项目以后,访问的方式是:http://localhost:9999/MonitorSystem/indexTo,特别注意...
用dwr3实现推送功能,文件就是该实现代码
dwr消息推送 dwr消息推送 dwr消息推送 dwr消息推送 dwr消息推送 dwr消息推送