第一步:下载dwr.jar、commons-logging.jar(我这里用的是dwr3.0)导入到自己的工程里。
第二步:修改web.xml,加入以下code
dwr-invoker org.directwebremoting.servlet.DwrServlet crossDomainSessionSecurity false allowScriptTagRemoting true classes java.lang.Object activeReverseAjaxEnabled true initApplicationScopeCreatorsAtStartup true maxWaitAfterWrite 3000 debug true logLevel WARN dwr-invoker /dwr/*
第三步:编写java类
1.MessagePush类
package com.dwr.messagemind.service; import javax.servlet.ServletException; import org.directwebremoting.ScriptSession; import org.directwebremoting.WebContextFactory; import com.dwr.messagemind.servlet.DwrScriptSessionManagerUtil; public class MessagePush { public void onPageLoad(String userId) { ScriptSession scriptSession = WebContextFactory.get().getScriptSession(); System.out.println("scriptSession:" + scriptSession); scriptSession.setAttribute("userId", userId); DwrScriptSessionManagerUtil dwrScriptSessionManagerUtil = new DwrScriptSessionManagerUtil(); try { dwrScriptSessionManagerUtil.init(); } catch (ServletException e) { e.printStackTrace(); } } }
2.DwrScriptSessionManagerUtil类
package com.dwr.messagemind.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpSession; import org.directwebremoting.Container; import org.directwebremoting.ServerContextFactory; import org.directwebremoting.WebContextFactory; import org.directwebremoting.event.ScriptSessionEvent; import org.directwebremoting.event.ScriptSessionListener; import org.directwebremoting.extend.ScriptSessionManager; import org.directwebremoting.servlet.DwrServlet; public class DwrScriptSessionManagerUtil extends DwrServlet { private static final long serialVersionUID = -7504612622407420071L; public void init() throws ServletException { Container container = ServerContextFactory.get().getContainer(); ScriptSessionManager manager = container.getBean(ScriptSessionManager.class); ScriptSessionListener listener = new ScriptSessionListener() { public void sessionCreated(ScriptSessionEvent ev) { HttpSession session = WebContextFactory.get().getSession(); //String userId = ((User) session.getAttribute("userinfo")).getHumanid() + ""; String userId = "123"; System.out.println("a ScriptSession is created!"); ev.getSession().setAttribute("userId", userId); } public void sessionDestroyed(ScriptSessionEvent ev) { System.out.println("a ScriptSession is distroyed"); } }; manager.addScriptSessionListener(listener); } }
3.DwrTool类(前台页面在需要推送的地方调用此类的方法)
package com.dwr.messagemind.service; import java.util.Collection; import org.directwebremoting.Browser; import org.directwebremoting.ScriptBuffer; import org.directwebremoting.ScriptSession; import org.directwebremoting.ScriptSessionFilter; public class DwrTool { public void sendMessageAuto(String userid, String message) { final String userId = userid; final String autoMessage = message; Browser.withAllSessionsFiltered(new ScriptSessionFilter() { public boolean match(ScriptSession session) { if (session.getAttribute("userId") == null) { return false; } else { return (session.getAttribute("userId")).equals(userId); } } }, new Runnable() { private ScriptBuffer script = new ScriptBuffer(); public void run() { script.appendCall("showMessage", autoMessage); Collectionsessions = Browser.getTargetSessions(); for (ScriptSession scriptSession : sessions) { scriptSession.addScript(script); } } }); } }
第四步:配置dwr.xml,在web.xml同级位置新建dwr.xml
第五步:编写jsp页面测试
接收页面需要做以下变动:
前两项为dwr配置,第三个调用js方法出发MessagePush.onPageLoad('')方法,注意MessagePush为在dwr中配置的类名,并且需要在页面引入MessagePush.js,这个为系统自动生成,不需要自己创建.engine.js、util.js也为系统自动生成,不需要自己创建,只许引入即可。
发送页面做以下配置
引入
在需要调用的地方调用js方法send('')即可。
注:因为dwr用的ajax反转方式实现推送,所以如果在ajax方法里调用send()是没有效果的,会报异常,当然在发送页面将dwr设置成同步方式可以发送成功(我实验的时候会先弹出一个错误窗口,但是还可以实现效果),只许在加入
onlocad="dwr.engine.setAsync(false);"
当然如果出现弹出错误窗口交互不好,继续想办法,既然不能在ajax里面调用,还希望成功的时候调用send(),那就用