实例分为两个页面

sendMsg.jsp页面 ,发送一段消息给后台 ,然后后台将消息推送到showMsg.jsp页面

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>dwr3</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>
		 <!-- 使用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>

sendMsg.java 推送类

package com.zf.dwr;

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;

public class SendMsg {
	
	@SuppressWarnings("deprecation")
	public void sendMsg(String msg){
		//得到上下文
		WebContext contex = WebContextFactory.get();
		
		//得到要推送到 的页面  dwr3为项目名称 , 一定要加上。
		Collection<ScriptSession> sessions = contex.getScriptSessionsByPage("/dwr3/showMsg.jsp");
		
		//不知道该怎么解释这个 , 
		Util util = new Util(sessions);
		
		//下面是创建一个javascript脚本 , 相当于在页面脚本中添加了一句  show(msg); 
		ScriptBuffer sb = new ScriptBuffer();
		sb.appendScript("show(");
		sb.appendData(msg);
		sb.appendScript(")");
		
		//推送
		util.addScript(sb);
	}
}

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="SendMsg">
  				<param name="class">com.zf.dwr.SendMsg</param>
  			</create>
  		</allow>
  </dwr>  

sendMsg.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">
<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="jquery-1.7.1.js"></script> 
<script type="text/javascript">
	$(function(){
		dwr.engine.setActiveReverseAjax(true);
		$("#but").click(function(){
			SendMsg.sendMsg($("#msg").val());
		});
	});
</script>
<title>Insert title here</title>
</head>  
<body>
	<input type="text" id="msg"  />
	<input type="button" value="发送" id="but" />
</body>
</html>

showMsg.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">
<script type="text/javascript" src="dwr/util.js"></script>  
<script type="text/javascript" src="dwr/engine.js"></script> 
<script type="text/javascript" src="jquery-1.7.1.js"></script> 
<script type="text/javascript">
	$(function(){
		//这句话千万不能少 ,表示允许使用推送技术
		dwr.engine.setActiveReverseAjax(true);
	});
	//这个函数是提供给后台推送的时候  调用的  
	function show(msg){
		$("#message").text(msg);
	}
</script>
<title>Insert title here</title>
</head>  
<body>
	<div id="message" style="width: 200px;height: 200px;border: 1px solid red ; text-align: center; padding: 5px;"></div>
</body>
</html> 




使用dwr推送技术时  需要有WebContext 对象,但是 WebContext 对象需要通过 WebContextFactory.get();获取 , 而WebContextFactory.get(); 方法只有当页面请求某个java方法时,在该方法中调用WebContextFactory.get(); 才能得到值。 否则是取不到值的。 

然后通过WebContext 获取指定的页面 。再向页面推送消息 。 

有一个问题是当后来又新开了页面,却收不到消息 。 

可以使用一个线程,去定时的更新Collection<ScriptSession> 对象


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐