web.xml中加入代码
<!-- DWR --> <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> <init-param> <param-name>pollAndCometEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>activeReverseAjaxEnabled</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>
dwr.xml中代码
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://directwebremoting.org/schema/dwr30.dtd"> <dwr> <!-- without allow,DWR isn't allowed to do anything --> <allow> <create creator="new" javascript="SendMsg"> <param name="class">com.action.Test</param> </create> <convert converter="bean" match="com.model.Books" javascript="Books"/> </allow> </dwr>
sendMsg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="/newtest/dwr/util.js"></script> <script type="text/javascript" src="/newtest/dwr/engine.js"></script> <script type="text/javascript" src="/newtest/dwr/interface/SendMsg.js"></script> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function(){ //表示允许使用推送技术 dwr.engine.setActiveReverseAjax(true); dwr.engine.setNotifyServerOnPageUnload(true); $("#but").click(function(){ SendMsg.sendMsg(11,$("#msg").val()); }); }); </script> <title>Insert title here</title> </head> <body> <input type="text" id="msg" /> <input type="button" value="发送" id="but" /> <div id="message" style="width: 200px;height: 200px;border: 1px solid red ; text-align: center; padding: 5px;"></div> </body> </html>
showMsg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="/newtest/dwr/util.js"></script> <script type="text/javascript" src="/newtest/dwr/engine.js"></script> <script type="text/javascript" src="/newtest/dwr/interface/SendMsg.js"></script> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function(){ //表示允许使用推送技术 dwr.engine.setActiveReverseAjax(true); dwr.engine.setNotifyServerOnPageUnload(true); }); //通过该方法与后台交互,确保推送时能找到指定用户 function onPageLoad(){ var userId = '11'; SendMsg.saveid(userId); } //推送信息 function showMessage(autoMessage){ $("#message").text(autoMessage); } </script> <title>Insert title here</title> </head> <body onload="onPageLoad();"> <div id="message" style="width: 200px;height: 200px;border: 1px solid red ; text-align: center; padding: 5px;"></div> </body> </html>
Test.java代码
package com.action; import java.util.Collection; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.directwebremoting.Browser; import org.directwebremoting.ScriptBuffer; import org.directwebremoting.ScriptSession; import org.directwebremoting.ScriptSessionFilter; import org.directwebremoting.WebContextFactory; public class Test { public void sendMsg(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); Collection<ScriptSession> sessions = Browser.getTargetSessions(); for (ScriptSession scriptSession : sessions){ scriptSession.addScript(script); } } }); } public void saveid(String userId){ HttpServletRequest request = ServletActionContext.getRequest(); request.getSession().setAttribute("userId",userId); ScriptSession scriptSession = WebContextFactory.get().getScriptSession(); scriptSession.setAttribute("userId",userId); } }原文链接:https://www.f2er.com/ajax/166319.html