DWR消息推送实例
一、DWR简介
DWR(DirectWebRemoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站。它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一样。
二、DWR下载地址
DWR的资源包可以从DWR的官网http://directwebremoting.org/dwr/获得,dwr.jar包,是DWR实现的主要依赖包。
官方还提供了DWR的示例,用户可到官网下载dwr.war文件,里面包含了官方提供的Demo。
三、DWR实例
(一)示例说明:
该示例实现了DWR消息推送功能。包含消息发送端和消息接收端。
(二)程序代码:
① 代码截图
② 所需的jar包:
a.commons-logging.jar
b.Dwr.jar
c.log4j-1.2.14.jar
③ 代码实现:
a.在Web.xml文件中配置DWR:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns=""
xsi:schemaLocation=""
id="WebApp_ID" version="3.0">
<display-name>dwrpush</display-name>
<welcome-file-list>
<welcome-file>client.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<description>调试DWR,发布系统时,应将其设为false</description>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- 使用polling和comet的方式 -->
<init-param>
<param-name>pollAndCometEnabled</param-name>
<param-value>true</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>
</web-app>
b.Java消息发送代码:
SendMsg.java
package com.dwr.service; import java.util.Collection; import org.directwebremoting.ScriptBuffer; import org.directwebremoting.ScriptSession; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; import org.directwebremoting.proxy.dwr.Util; /** * 消息发送类 * */ @SuppressWarnings("deprecation") public class SendMsg { /** * 消息发送方法 * @param msg //要发送的消息 */ public void sendMsg(String msg){ WebContext contex=WebContextFactory.get(); //获取client.jsp页面的ScriptSession集合,注意:"/dwrpush/client.jsp" Collection<ScriptSession> sessions=contex.getScriptSessionsByPage("/dwrpush/client.jsp"); Util util=new Util(sessions); ScriptBuffer sb=new ScriptBuffer(); sb.appendScript("show("); sb.appendData(msg); sb.appendScript(")"); util.addScript(sb); } }
c.在WEB-INF目录下创建dwr.xml文件,配置SendMsg类:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
"http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<create creator="new" javascript="SendMsg">
<param name="class">com.dwr.service.SendMsg</param>
</create>
</allow>
</dwr>
d.消息接收端实现:
Client.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Client</title> <script type="text/javascript" src="dwr/util.js"></script> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/interface/SendMsg.js"></script> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript"> $(function(){ dwr.engine.setActiveReverseAjax(true); }); /* * 后台推送消息调用函数 */ function show(msg){ $("#message").text(msg); } </script> </head> <body> <h1>消息接收端</h1> <div id="message" style="width: 300px; height: 100px; border: 1px solid #CDCDCD; text-align: left; padding: 5px;"></div> </body> </html>
e.消息放送端实现:
Manager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Manager</title> <script type="text/javascript" src="dwr/util.js"></script> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/interface/SendMsg.js"></script> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript"> $(function(){ dwr.engine.setActiveReverseAjax(true); $("#send").click(function(){ SendMsg.sendMsg($("#msg").val()); }); }); </script> </head> <body> <h1>消息发送端</h1> 向客户端发送消息: <input type="text" name="msg" id="msg"/> <input type="button" value="发送" id="send"/> </body> </html>
④ 运行结果:
四、总结
DWR的功能还是比较强大的,此实例实现了dwr直接调用Java代码,后台向指定页面推送消息。DWR消息推送可以方便地应用到你的web应用中!