`
vista_move
  • 浏览: 29157 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

DWR后台推送技术

    博客分类:
  • java
阅读更多
最近工作需要写一个后台推送消息到浏览器提示用户操作的功能,在网上摸索了几天,有的是比较乱的,会造成一些误导,现在把自己应用笔记写下来跟大家分享。

第一步:下载dwr.jar、commons-logging.jar(我这里用的是dwr3.0)导入到自己的工程里。

第二步:修改web.xml,加入以下code
<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>
  <servlet-mapping>
  
    <servlet-name>dwr-invoker</servlet-name>
    
    <url-pattern>/dwr/*</url-pattern>
  
  </servlet-mapping>

第三步:编写java类

1.MessagePush类
package com.dwr.messagemind.service;

import javax.servlet.ServletException;

import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContextFactory;

import com.dwr.messagemind.servlet.DwrScriptSessionManagerUtil;


public class MessagePush {

	 public void onPageLoad(String userId) {

         ScriptSession scriptSession = WebContextFactory.get().getScriptSession();

         System.out.println("scriptSession:" + scriptSession);
         
         scriptSession.setAttribute("userId", userId);

         DwrScriptSessionManagerUtil dwrScriptSessionManagerUtil = new DwrScriptSessionManagerUtil();

         try {

        	 dwrScriptSessionManagerUtil.init();

         } catch (ServletException e) {

                e.printStackTrace();

         }

  }
}



2.DwrScriptSessionManagerUtil类
package com.dwr.messagemind.servlet;

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() + "";
				String userId = "123";

				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);

	}

}



3.DwrTool类(前台页面在需要推送的地方调用此类的方法)
package com.dwr.messagemind.service;

import java.util.Collection;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;

public class DwrTool {

	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);

				}

			}

		});

	}
}



第四步:配置dwr.xml,在web.xml同级位置新建dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.dwr.messagemind.service.MessagePush"/>
		</create>
		<create creator="new" javascript="DwrTool">
	  		<param name="class" value="com.dwr.messagemind.service.DwrTool"/>
		</create>
	</allow>
</dwr>


第五步:编写jsp页面测试

接收页面需要做以下变动:
<script type="text/javascript" src="<%=path%>/dwr/engine.js"></script>
        <script type="text/javascript" src="<%=path%>/dwr/util.js"></script>
        <script type="text/javascript" src="<%=path%>/dwr/interface/MessagePush.js"></script>
<script type="text/javascript">
			function onPageLoad(){
             	MessagePush.onPageLoad("123");
          	}
          	function showMessage(autoMessage) {
          		alert(autoMessage);
          		document.all.bb.innerHTML = autoMessage;
          	}
		</script>


<body onload="dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true);onPageLoad();">


前两项为dwr配置,第三个调用js方法出发MessagePush.onPageLoad('')方法,注意MessagePush为在dwr中配置的类名,并且需要在页面引入MessagePush.js,这个为系统自动生成,不需要自己创建.engine.js、util.js也为系统自动生成,不需要自己创建,只许引入即可。

发送页面做以下配置

引入
		<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
        <script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
        <script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/DwrTool.js"></script>

<script type="text/javascript">
		
		function send(message) {
			DwrTool.sendMessageAuto("123", message);
		}
</script>

在需要调用的地方调用js方法send('')即可。

注:因为dwr用的ajax反转方式实现推送,所以如果在ajax方法里调用send()是没有效果的,会报异常,当然在发送页面将dwr设置成同步方式可以发送成功(我实验的时候会先弹出一个错误窗口,但是还可以实现效果),只许在<body>加入
onlocad="dwr.engine.setAsync(false);"


当然如果出现弹出错误窗口交互不好,继续想办法,既然不能在ajax里面调用,还希望成功的时候调用send(),那就用<iframe>吧,

<form action="你要请求的servlet" method="post" id="inputform" name="inputform" target="hidden_frame">
			<iframe id="hidden_frame" name="hidden_frame" style="display: none;"></iframe>


接下来提交form到后台servlet,后台处理成功之后调用send();
response.getWriter().print("<script>");
				response.getWriter().print("parent.send('成功了');");
				response.getWriter().print("</script>");


至此已经都说完了,如果有疑问请留言吧。
1
0
分享到:
评论
2 楼 vista_move 2014-11-03  
不好意思到现在才回复你,好久没登陆了。下面回答你的疑问,因为在ajax回调方法了用DWR send会失败,所以换一种方式,其实这就是一种伪ajax:
1.在你的form属性中加入
target="hidden_frame"

2.在页面中加入
<iframe id="hidden_frame" name="hidden_frame" style="display: none;"></iframe>  

解释一下,这段代码的效果就是在不刷新页面的情况下完成表单的提交操作(这不就是ajax嘛,嘿嘿),如果不这么操作那么当前页面的URL地址就变成你的form action="URL"地址了
1 楼 jzzwy 2014-03-06  
不错的文章 ,
虽然最后你用form 和Iframe 和后台发送 不太明白 ,但总算成功,没出现问题,
我直接点击按钮 send的

相关推荐

Global site tag (gtag.js) - Google Analytics