页面消息推送:
第一步:导入需要的两个Jar包:(下载地址:http://directwebremoting.org/dwr/downloads/index.html)
·dwr.jar
commons-logging-1.1.1.jar
第二步:在web.xml文件中添加(目录在WebContent/WEB-INF/lib/下)
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <!-- 指定DWR核心Servlet处于调试状态 --> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <!-- 设置使用反向Ajax技术 --> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <!--长连接只保持时间 --> <init-param> <param-name>maxWaitAfterWrite</param-name> <param-value>3000</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>classes</param-name> <param-value>java.lang.Object</param-value> </init-param> <init-param> <param-name>initApplicationScopeCreatorsAtStartup</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>logLevel</param-name> <param-value>WARN</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
第三步:创建dwr.xml文件 (放在与web.xml同一个目录下)
<!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" value="com.videocon.util.MessagePush"/> </create> <create creator="new" javascript="TestPush"> <param name="class" value="com.videocon.util.Test"/> </create> </allow> </dwr>
第四步:根据dwr.xml文件暴露出的class,写具体的类
MessagePush.javapublic class MessagePush{ public void onPageLoad(String userName) { ScriptSession scriptSession = WebContextFactory.get().getScriptSession(); scriptSession.setAttribute(userName,userName); DwrScriptSessionManagerUtil dwrScriptSessionManagerUtil = new DwrScriptSessionManagerUtil(); try { dwrScriptSessionManagerUtil.init(); System.out.println("cacaca"); } catch (ServletException e) { e.printStackTrace(); } } }Test.java
public class Test{ public static void sendMessageAuto(String username,String message){ final String userName = username; final String autoMessage = message; Browser.withAllSessionsFiltered(new ScriptSessionFilter() { public boolean match(ScriptSession session){ System.out.println(session.getAttribute("userName")); if (session.getAttribute("userName") == null) return false; else return (session.getAttribute("userName")).equals(userName); } },new Runnable(){ private ScriptBuffer script = new ScriptBuffer(); public void run(){ System.out.println("进入方法。。。"); script.appendCall("showMessage",autoMessage); Collection<ScriptSession> sessions = Browser.getTargetSessions(); for (ScriptSession scriptSession : sessions){ scriptSession.addScript(script); } } }); } }还要添加一个工具类DwrScriptSessionManagerUtil.java
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 userName =((PersonInfo) session.getAttribute("person")).getUsername()+""; System.out.println("a ScriptSession is created!"); ev.getSession().setAttribute("userName",userName); } public void sessionDestroyed(ScriptSessionEvent ev) { System.out.println("a ScriptSession is distroyed"); } }; manager.addScriptSessionListener(listener); } }
第五步:写推送端的界面:
1.先引入js,engine.js 文件 是dwr的引擎文件,util.js 是dwr自带的工具js,TestPush.js就是你在dwr.xml文件中配置的javaScript的别名,这里只需要引入即可.
<script type="text/javascript" src="${ctx!}/dwr/engine.js"></script> <script type="text/javascript" src="${ctx!}/dwr/util.js"></script> <script type="text/javascript" src="${ctx!}/dwr/interface/TestPush.js"></script>2.页面js代码
$("#button").click(function(){ test(); }); function test() { var msg = $("#username").val(); console.log("推送中。。。"+msg); TestPush.sendMessageAuto(msg,"哈哈哈"); }
第六步:写接收端的界面
1.先引入js,MessagePush.js就是你在dwr.xml文件中配置的javaScript的别名,这里只需要引入即可.
<script type="text/javascript" src="${ctx!}/dwr/engine.js"></script> <script type="text/javascript" src="${ctx!}/dwr/util.js"></script> <script type="text/javascript" src="${ctx!}/dwr/interface/MessagePush.js"></script>2.页面代码
$(document).ready(function(){ onPageLoad(); dwr.engine.setActiveReverseAjax(true); dwr.engine.setNotifyServerOnPageUnload(true); }); //通过该方法与后台交互,确保推送时能找到指定用户 function onPageLoad(){ var xx = 'username'; MessagePush.onPageLoad(xx); } //推送信息 function showMessage(autoMessage){ alert(autoMessage);//获取推送信息 }原文链接:https://www.f2er.com/ajax/161005.html