1、新建web项目
2、导入jar包(commons-logging-1.2.jar、dwr3.0.jar)
3、配置web.xml,并在web.xml同目录下创建dwr.xml
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<init-param>
<param-name> crossDomainSessionSecurity</param-name >
<param-value> false</param-value >
</init-param>
<init-param>
<param-name> allowScriptTagRemoting</param-name >
<param-value> true</param-value >
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name> dwr-invoker </servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
dwr.xml:
<?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= "messagePush">
<param name="class" >sugar.dwr.MessagePush</param>
</create>
</allow>
</dwr>
4、编写java类
package sugar.dwr;
/**
* 本节Demo实现的基本功能:
* 在之前的的代码基础上修改,
* 实现将index.jsp输入框中的推送文本显示在其它打开的jsp页面上
* 目前流行的服务器推送技术
*/
import java.util.Collection;
import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
public class MessagePush {
public void send(final String content){
Runnable run = new Runnable(){
private ScriptBuffer script = new ScriptBuffer();
public void run() {
//设置要调用的 js及参数
script.appendCall("show" , content);
//得到所有ScriptSession
Collection<ScriptSession> sessions = Browser.getTargetSessions();
//遍历每一个ScriptSession
for (ScriptSession scriptSession : sessions){
scriptSession.addScript( script);
}
}
};
//执行推送
Browser. withAllSessions(run);
}
}
5、编写页面
index.xml:
<%@ page language= "java" import ="java.util.*" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head >
<title >DWR</title>
<script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
<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/messagePush.js" ></script>
</head >
<body >
<table border="0" >
<tr>
<td><input id ="content" type="text"/></td>
<td><input id ="send" type="button" value= "send"/></td >
</tr>
</table>
<script type="text/javascript" >
$( "#send").click(function(){
var content = $("#content" ).val();
messagePush.send(content);
});
</script>
</body >
</html>
receiver.xml:
<%@ page language= "java" import ="java.util.*" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head >
<title>dwr接收</title >
<script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
<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/messagePush.js" ></script>
</head >
<body>dwr接收<br/>
<div id="content" style=" width: 200px ;height: 30px;border : 1px solid ; text-align: center ; padding: 5px;"></div>
<script type="text/javascript" >
//这个方法用来启动该页面的ReverseAjax功能
dwr.engine.setActiveReverseAjax(true);
//设置在页面关闭时,通知服务端销毁会话
dwr.engine.setNotifyServerOnPageUnload(true);
//这个函数是提供给后台推送的时候 调用的
function show(content){
$( "#content" ).text(content);
}
</script>
</body >
</html>
运行项目,先打开http://******:8080/ServerPush/receiver.jsp
然后在index.xml中输入发送的内容,即可发送并显示到receiver.jsp页面上。