加入dwr3.0.jar包
Web.xml配置:
1. <servlet>
2.
3. <servlet-name>dwr-invoker</servlet-name>
4.
5. <servlet-class>
6.
7. org.directwebremoting.servlet.DwrServlet
8.
9. </servlet-class>
10.
11. <init-param>
12.
13. <param-name>crossDomainSessionSecurity</param-name>
14.
15. <param-value>false</param-value>
16.
17. </init-param>
18.
19. <init-param>
20.
21. <param-name>allowScriptTagRemoting</param-name>
22.
23. <param-value>true</param-value>
24.
25. </init-param>
26.
27. <init-param>
28.
29. <param-name>classes</param-name>
30.
31. <param-value>java.lang.Object</param-value>
32.
33. </init-param>
34.
35. <init-param>
36.
37. <param-name>activeReverseAjaxEnabled</param-name>
38.
39. <param-value>true</param-value>
40.
41. </init-param>
42.
43. <init-param>
44.
45. <param-name>initApplicationScopeCreatorsAtStartup</param-name>
46.
47. <param-value>true</param-value>
48.
49. </init-param>
50.
51. <init-param>
52.
53. <param-name>maxWaitAfterWrite</param-name>
54.
55. <param-value>3000</param-value>
56.
57. </init-param>
58.
59. <init-param>
60.
61. <param-name>debug</param-name>
62.
63. <param-value>true</param-value>
64.
65. </init-param>
66.
67. <init-param>
68.
69. <param-name>logLevel</param-name>
70.
71. <param-value>WARN</param-value>
72.
73. </init-param>
74.
75. </servlet>
Dwr.xml配置
1. <!DOCTYPE dwr PUBLIC
2. "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
3. "http://getahead.org/dwr/dwr30.dtd">
4.
5. <dwr>
6. <allow>
7. <create creator="new" javascript="MessagePush">
8. <param name="class" value="com.huatech.messageremind.service.MessagePush"/>
9. </create>
10. <create creator="new" javascript="TestPush">
11. <param name="class" value="com.huatech.messageremind.service.Test"/>
12. </create>
13. </allow>
14. </dwr>
要推送的页面,onload是初始化页面的时候加载dwr配置,userid会传入到后台MessagePush().onPageLoad(userId)方法里
1. <html>
2. <hread></hread>
3. <script type='text/javascript' src='dwr/engine.js'></script>
4. <script type='text/javascript' src='dwr/util.js'></script>
5. <script type="text/javascript" src="dwr/interface/MessagePush.js"></script>
6.
7. <script type="text/javascript">
8. //通过该方法与后台交互,确保推送时能找到指定用户
9. function onPageLoad(){
10. var userId = '${userinfo.humanid}';
11. MessagePush.onPageLoad(userId);
12.
13. }
14. //推送信息
15. function showMessage(autoMessage){
16. alert(autoMessage);
17.
18. }
19. </script>
20. <body οnlοad="onPageLoad();dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true);;">
21. This is my DWR DEOM page. <hr>
22. <br>
23. <div id="DemoDiv">demo</div>
24. </body>
25. </html>
建立MessagePush.java
1. public class MessagePush{
2. public void onPageLoad(String userId) {
3.
4. ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
5.
6. scriptSession.setAttribute(userId, userId);
7.
8. DwrScriptSessionManagerUtil dwrScriptSessionManagerUtil = new DwrScriptSessionManagerUtil();
9.
10. try {
11.
12. dwrScriptSessionManagerUtil.init();
13. System.out.println("cacaca");
14.
15. } catch (ServletException e) {
16.
17. e.printStackTrace();
18.
19. }
20.
21. }
DwrScriptSessionManagerUtil().init()这个方法会获得你的session里的值
1. public class DwrScriptSessionManagerUtil extends DwrServlet{
2.
3. private static final long serialVersionUID = -7504612622407420071L;
4.
5. public void init()throws ServletException {
6.
7. Container container = ServerContextFactory.get().getContainer();
8. ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
9. ScriptSessionListener listener = new ScriptSessionListener() {
10. public void sessionCreated(ScriptSessionEvent ev) {
11. HttpSession session = WebContextFactory.get().getSession();
12.
13. String userId =((User) session.getAttribute("userinfo")).getHumanid()+"";
14. System.out.println("a ScriptSession is created!");
15. ev.getSession().setAttribute("userId", userId);
16. }
17. public void sessionDestroyed(ScriptSessionEvent ev) {
18. System.out.println("a ScriptSession is distroyed");
19. }
20. };
21. manager.addScriptSessionListener(listener);
22. }
23. }
这个类的重点是showMessage函数,是指你要调用页面的那个js方法,后面跟的是参数,以逗号分割
1. public class Test{
2. public void sendMessageAuto(String userid, String message){
3.
4. final String userId = userid;
5. final String autoMessage = message;
6. Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
7. public boolean match(ScriptSession session){
8. if (session.getAttribute("userId") == null)
9. return false;
10. else
11. return (session.getAttribute("userId")).equals(userId);
12. }
13. }, new Runnable(){
14.
15. private ScriptBuffer script = new ScriptBuffer();
16.
17. public void run(){
18.
19. script.appendCall("showMessage", autoMessage);
20.
21. Collection<ScriptSession> sessions = Browser
22.
23. .getTargetSessions();
24.
25. for (ScriptSession scriptSession : sessions){
26. scriptSession.addScript(script);
27. }
28. }
29. });
30. }
31. }
被推送的jsp页面
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2. <%
3. String path = request.getContextPath();
4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5. %>
6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
7. <html>
8. <head>
9. <base href="<%=basePath%>">
10. <title>My JSP 'MyJsp.jsp' starting page</title>
11. <meta http-equiv="pragma" content="no-cache">
12. <meta http-equiv="cache-control" content="no-cache">
13. <meta http-equiv="expires" content="0">
14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
15. <meta http-equiv="description" content="This is my page">
16. <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.5.1.js"></script>
17. <script type='text/javascript' src='dwr/engine.js'></script>
18. <script type='text/javascript' src='dwr/util.js'></script>
19. <script type='text/javascript' src='dwr/interface/TestPush.js'></script>
20.
21. <script type="text/javascript">
22.
23. function test() {
24. var msg = document.getElementById("msgId").value;
25. //msg = {msgId: '1', context: $("#msgContext").val()};
26. TestPush.sendMessageAuto(msg,"哈哈哈");
27.
28. }
29. </script>
30. </head>
31.
32. <body>
33. id : <input type="text" name="msgId" id="msgId" /> <br />
34.
35. <input type="button" value="Send" οnclick="test()" />
36.
37. </body>
38. </html>
Web.xml配置:
1. <servlet>
2.
3. <servlet-name>dwr-invoker</servlet-name>
4.
5. <servlet-class>
6.
7. org.directwebremoting.servlet.DwrServlet
8.
9. </servlet-class>
10.
11. <init-param>
12.
13. <param-name>crossDomainSessionSecurity</param-name>
14.
15. <param-value>false</param-value>
16.
17. </init-param>
18.
19. <init-param>
20.
21. <param-name>allowScriptTagRemoting</param-name>
22.
23. <param-value>true</param-value>
24.
25. </init-param>
26.
27. <init-param>
28.
29. <param-name>classes</param-name>
30.
31. <param-value>java.lang.Object</param-value>
32.
33. </init-param>
34.
35. <init-param>
36.
37. <param-name>activeReverseAjaxEnabled</param-name>
38.
39. <param-value>true</param-value>
40.
41. </init-param>
42.
43. <init-param>
44.
45. <param-name>initApplicationScopeCreatorsAtStartup</param-name>
46.
47. <param-value>true</param-value>
48.
49. </init-param>
50.
51. <init-param>
52.
53. <param-name>maxWaitAfterWrite</param-name>
54.
55. <param-value>3000</param-value>
56.
57. </init-param>
58.
59. <init-param>
60.
61. <param-name>debug</param-name>
62.
63. <param-value>true</param-value>
64.
65. </init-param>
66.
67. <init-param>
68.
69. <param-name>logLevel</param-name>
70.
71. <param-value>WARN</param-value>
72.
73. </init-param>
74.
75. </servlet>
Dwr.xml配置
1. <!DOCTYPE dwr PUBLIC
2. "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
3. "http://getahead.org/dwr/dwr30.dtd">
4.
5. <dwr>
6. <allow>
7. <create creator="new" javascript="MessagePush">
8. <param name="class" value="com.huatech.messageremind.service.MessagePush"/>
9. </create>
10. <create creator="new" javascript="TestPush">
11. <param name="class" value="com.huatech.messageremind.service.Test"/>
12. </create>
13. </allow>
14. </dwr>
要推送的页面,onload是初始化页面的时候加载dwr配置,userid会传入到后台MessagePush().onPageLoad(userId)方法里
1. <html>
2. <hread></hread>
3. <script type='text/javascript' src='dwr/engine.js'></script>
4. <script type='text/javascript' src='dwr/util.js'></script>
5. <script type="text/javascript" src="dwr/interface/MessagePush.js"></script>
6.
7. <script type="text/javascript">
8. //通过该方法与后台交互,确保推送时能找到指定用户
9. function onPageLoad(){
10. var userId = '${userinfo.humanid}';
11. MessagePush.onPageLoad(userId);
12.
13. }
14. //推送信息
15. function showMessage(autoMessage){
16. alert(autoMessage);
17.
18. }
19. </script>
20. <body οnlοad="onPageLoad();dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true);;">
21. This is my DWR DEOM page. <hr>
22. <br>
23. <div id="DemoDiv">demo</div>
24. </body>
25. </html>
建立MessagePush.java
1. public class MessagePush{
2. public void onPageLoad(String userId) {
3.
4. ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
5.
6. scriptSession.setAttribute(userId, userId);
7.
8. DwrScriptSessionManagerUtil dwrScriptSessionManagerUtil = new DwrScriptSessionManagerUtil();
9.
10. try {
11.
12. dwrScriptSessionManagerUtil.init();
13. System.out.println("cacaca");
14.
15. } catch (ServletException e) {
16.
17. e.printStackTrace();
18.
19. }
20.
21. }
DwrScriptSessionManagerUtil().init()这个方法会获得你的session里的值
1. public class DwrScriptSessionManagerUtil extends DwrServlet{
2.
3. private static final long serialVersionUID = -7504612622407420071L;
4.
5. public void init()throws ServletException {
6.
7. Container container = ServerContextFactory.get().getContainer();
8. ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
9. ScriptSessionListener listener = new ScriptSessionListener() {
10. public void sessionCreated(ScriptSessionEvent ev) {
11. HttpSession session = WebContextFactory.get().getSession();
12.
13. String userId =((User) session.getAttribute("userinfo")).getHumanid()+"";
14. System.out.println("a ScriptSession is created!");
15. ev.getSession().setAttribute("userId", userId);
16. }
17. public void sessionDestroyed(ScriptSessionEvent ev) {
18. System.out.println("a ScriptSession is distroyed");
19. }
20. };
21. manager.addScriptSessionListener(listener);
22. }
23. }
这个类的重点是showMessage函数,是指你要调用页面的那个js方法,后面跟的是参数,以逗号分割
1. public class Test{
2. public void sendMessageAuto(String userid, String message){
3.
4. final String userId = userid;
5. final String autoMessage = message;
6. Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
7. public boolean match(ScriptSession session){
8. if (session.getAttribute("userId") == null)
9. return false;
10. else
11. return (session.getAttribute("userId")).equals(userId);
12. }
13. }, new Runnable(){
14.
15. private ScriptBuffer script = new ScriptBuffer();
16.
17. public void run(){
18.
19. script.appendCall("showMessage", autoMessage);
20.
21. Collection<ScriptSession> sessions = Browser
22.
23. .getTargetSessions();
24.
25. for (ScriptSession scriptSession : sessions){
26. scriptSession.addScript(script);
27. }
28. }
29. });
30. }
31. }
被推送的jsp页面
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2. <%
3. String path = request.getContextPath();
4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5. %>
6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
7. <html>
8. <head>
9. <base href="<%=basePath%>">
10. <title>My JSP 'MyJsp.jsp' starting page</title>
11. <meta http-equiv="pragma" content="no-cache">
12. <meta http-equiv="cache-control" content="no-cache">
13. <meta http-equiv="expires" content="0">
14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
15. <meta http-equiv="description" content="This is my page">
16. <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.5.1.js"></script>
17. <script type='text/javascript' src='dwr/engine.js'></script>
18. <script type='text/javascript' src='dwr/util.js'></script>
19. <script type='text/javascript' src='dwr/interface/TestPush.js'></script>
20.
21. <script type="text/javascript">
22.
23. function test() {
24. var msg = document.getElementById("msgId").value;
25. //msg = {msgId: '1', context: $("#msgContext").val()};
26. TestPush.sendMessageAuto(msg,"哈哈哈");
27.
28. }
29. </script>
30. </head>
31.
32. <body>
33. id : <input type="text" name="msgId" id="msgId" /> <br />
34.
35. <input type="button" value="Send" οnclick="test()" />
36.
37. </body>
38. </html>