时间:2021-07-01 10:21:17 帮助过:29人阅读
1、web.xml 配置,在web.xml里面添加如下内容:
DWR Servlet dwr-invoker org.directwebremoting.servlet.DwrServlet debug true pollAndCometEnabled true 1 dwr-invoker /dwr/*
iwco.dwrs.SendMsg
3、java类 SendMsg.java
package iwco.dwrs;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为项目名称 , 一定要加上。 Collectionsessions = contex.getScriptSessionsByPage("/dwrTest/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); } }
sendMsg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>sendMsg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>showMsg