Java Dwr3实现消息推送步骤详解

DWR包含两个主要的部分:允许JavaScript从WEB服务器上一个遵循了AJAX原则的Servlet中获取数据.另外一方面一个JavaScript库可以帮助网站开发人员轻松地利用获取的数据来动态改变网页的内容,DWR采取了一个类似AJAX的新方法来动态生成基于JAVA类的JavaScript代码。这样WEB开发人员就可以在JavaScript里使用Java代码,就像它们是浏览器的本地代码(客户端代码)一样;但是Java代码运行在WEB服务器端而且可以*访问WEB 服务器的资源。出于安全的理由,WEB开发者必须适当地配置哪些Java类可以安全的被外部使用。下面讲解一下...

1、在工程中引入dwr.jar,之后修改配置web.xml文件,添加配置具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <servlet-class>
            org.directwebremoting.servlet.DwrServlet
        </servlet-class>
        <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>activeReverseAjaxEnabled</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
           <param-name>initApplicationScopeCreatorsAtStartup</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>debug</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>logLevel</param-name>
            <param-value>WARN</param-value>
        </init-param>
    </servlet>

2、在web.xml统计目录下新增dwr.xml文件,具体内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE dwr PUBLIC
          "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
          "http://getahead.org/dwr/dwr30.dtd">
     <dwr>
          <alow>
               <create creator="new" javascript="MessagePush">
                 <param name="class" value="com.yoodb.service.MessagePush"/>
              </create>
          <create creator="new" javascript="TestPush">  
                  <param name="class" value="com.yoodb.service.TestPush"/>  
              </create>
          </alow>
     </dwr>

MessagePush在页面的javascript中使用,com.yoodb.service.MessagePush实现了想要调用的方法,其中MessagePush.java对被推送页面开放的java类,Test.java是对推送页面开放的java类。在javascript中使用MessagePush.java类中实现的方法,即可在dwr中调用。

3、引入JavaScript文件,具体如下:

1
2
3
<script type="text/javascript" src="<%=basepath%>dwr/engine.js"></script>
<script type="text/javascript" src="<%=basepath%>dwr/util.js"></script>
<script type="text/javascript" src="<%=basepath%>dwr/interface/MessagePush.js"></script>

注意:

1)dwr.xml配置的javascript中engine.js和util.js是必须引入的文件。

2)在任何一个用户登录的时候,都需要将其userId或者其他唯一性标识放入session中,我放的是userId,这里就以userId为唯一性标识。

3)在载入想推送的页面时,需要onload一个我在MessagePush类中实现的方法,当然了,需要使用dwr调用

被推送html页面具体内容代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <base href="<%=basePath%>">  
    <title>DWR  DEMO</title>  
    <meta http-equiv="pragma" content="no-cache">  
    <meta http-equiv="cache-control" content="no-cache">  
    <meta http-equiv="expires" content="0">      
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    <meta http-equiv="description" content="This is my page">  
  </head>  
  <script type='text/javascript' src='dwr/engine.js'></script>  
  <script type='text/javascript' src='dwr/util.js'></script>  
  <script type="text/javascript" src="dwr/interface/MessagePush.js"></script>  
   
  <script type="text/javascript">  
        //通过该方法与后台交互,确保推送时能找到指定用户  
         function onPageLoad(){  
            var userId = '${userinfo.userId}';  
            MessagePush.onPageLoad(userId);  
          }  
         //推送信息  
         function showMessage(autoMessage){  
                alert(autoMessage);      
        }  
  </script>  
  <body onload="onPageLoad();dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true);;">   
    This is my DWR DEOM page. <hr>  
    <br>  
    <div id="DemoDiv">demo</div>  
  </body>  
</html>

其中MessagePush.java文件中实现方法,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class MessagePush{  
    public void onPageLoad(String userId) {  
           ScriptSession scriptSession = WebContextFactory.get().getScriptSession();  
         scriptSession.setAttribute(userId, userId);  
           DwrScriptSessionManagerUtil dwrScriptSessionManagerUtil = new DwrScriptSessionManagerUtil();  
         try {  
                dwrScriptSessionManagerUtil.init();  
                System.out.println("cacaca");  
         catch (ServletException e) {  
                e.printStackTrace();  
           }  
    }  
}

对于onPageLoad()方法中DwrScriptSessionManagerUtil类的实现,具体如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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()+"";  
                         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);  
    }  
}

4、推送html页面具体内容代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <base href="<%=basePath%>">  
    <title>My JSP 'MyJsp.jsp' starting page</title>  
    <meta http-equiv="pragma" content="no-cache">  
    <meta http-equiv="cache-control" content="no-cache">  
    <meta http-equiv="expires" content="0">      
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    <meta http-equiv="description" content="This is my page">  
    <script type='text/javascript' src='dwr/engine.js'></script>  
    <script type='text/javascript' src='dwr/util.js'></script>  
    <script type='text/javascript' src='dwr/interface/TestPush.js'></script>  
       
    <script type="text/javascript">  
       
    function testPush() {  
        var msg = document.getElementById("msgId").value;  
        TestPush.sendMessageAuto(msg,"www.yoodb.com");  
           
    }  
    </script>  
  </head>  
     
  <body>  
    id值: <input type="text" name="msgId" id="msgId" /> <br />  
    <input type="button" value="Send" onclick="testPush()"  />  
  </body>  
</html>

其中TeshPush.java文件,具体内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class TestPush{  
    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);  
                   
                Collection<ScriptSession> sessions = Browser  
   
                .getTargetSessions();  
                   
                for (ScriptSession scriptSession : sessions){  
                    scriptSession.addScript(script);  
                }  
            }  
        });  
    }  
}