实现步骤:
第一、在项目中引入dwr.jar,然后在web.xml中进行配置,配置如下:
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</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>
<!DOCTYPEdwrPUBLIC "-//GetAheadLimited//DTDDirectWebRemoting3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <alow> <createcreator="new"javascript="MessagePush"> <paramname="class"value="com.pcitc.lw.service.MessagePush"/> </create> <createcreator="new"javascript="TestPush"> <paramname="class"value="com.pcitc.lw.service.Test"/> </create> </alow> </dwr>
dwr的基本配置指向具体实现类,MessagePush在页面的javascript中使用(被推送者),Test推送者
第三,被推送的页面
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <Metahttp-equiv="cache-control"content="no-cache"> </head> <scripttype='text/javascript'src='dwr/engine.js'></script> <scripttype='text/javascript'src='dwr/util.js'></script> <scripttype="text/javascript"src="dwr/interface/MessagePush.js"></script> <scripttype="text/javascript"> functiononPageLoad(){ varuserId='${userinfo.id}'; MessagePush.onPageLoad(userId); } //推送信息 functionshowMessage(mgs){ alert(mgs); } </script> <bodyonload="onPageLoad();dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true);"> <divid="DemoDiv">helloworld</div> </body> </html>
注意:页面加载时加载的函数setActiveReverseAjax和setNotifyServerOnPageUnload是dwr的,onPageLoad是将登陆的用户和scriptSession进行绑定,为以后的推送做准备。
第四 MessagePush类:
publicvoidonPageLoad(StringuserId){ ScriptSessionscriptSession=WebContextFactory.get().getScriptSession(); scriptSession.setAttribute(userId,userId); DwRSSessionUtilss=newDwRSSessionUtil(); try{ ss.init(); }catch(ServletExceptione){ e.printStackTrace(); } }
第五:DwRSSessionUtil :
publicclassDwRSSessionUtilextendsDwrServlet{ publicvoidinit()throwsServletException{ Containercontainer=ServerContextFactory.get().getContainer(); ScriptSessionManagermanager=container.getBean(ScriptSessionManager.class); ScriptSessionListenerlistener=newScriptSessionListener(){ publicvoidsessionCreated(ScriptSessionEventev){ HttpSessionsession=WebContextFactory.get().getSession(); StringuserId=((User)session.getAttribute("userinfo")).getId()+""; System.out.println("aScriptSessioniscreated!"); ev.getSession().setAttribute("userId",userId); } publicvoidsessionDestroyed(ScriptSessionEventev){ System.out.println("aScriptSessionisdistroyed"); } }; manager.addScriptSessionListener(listener); } }
第五 JSP页面:
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <Metahttp-equiv="cache-control"content="no-cache"> <Metahttp-equiv="expires"content="0" <scripttype="text/javascript"src="js/jquery-2.1.js"></script> <scripttype='text/javascript'src='dwr/engine.js'></script> <scripttype='text/javascript'src='dwr/util.js'></script> <scripttype='text/javascript'src='dwr/interface/TestPush.js'></script> <scripttype="text/javascript"> functiontest(){ varmsg=document.getElementById("msgId").value; TestPush.sendMessageAuto(msg,"收到没"); } </script> </head> <body> id:<inputtype="text"name="msgId"id="msgId"/><br/> <inputtype="button"value="你好"onclick="test()"/> </body> </html>
第六:推送代码:
publicclassTest{ publicvoidsendMessageAuto(Stringuserid,Stringmessage){ finalStringuserId=userid; finalStringautoMessage=message; Browser.withAllSessionsFiltered(newScriptSessionFilter(){ publicbooleanmatch(ScriptSessionsession){ if(session.getAttribute("userId")==null){ returnfalse; }else{ return(session.getAttribute("userId")).equals(userId); } } },newRunnable(){ privateScriptBuffers=newScriptBuffer(); publicvoidrun(){ s.appendCall("showMessage",autoMessage); Collection<ScriptSession>sessions=Browser.getTargetSessions(); for(ScriptSessionscriptSession:sessions){ scriptSession.addScript(s); } } }); } }原文链接:https://www.f2er.com/ajax/162485.html