当前位置: 代码迷 >> Web前端 >> DWR兑现简单推送服务的示例
  详细解决方案

DWR兑现简单推送服务的示例

热度:158   发布时间:2013-01-08 14:02:14.0
DWR实现简单推送服务的示例
/*
 * Copyright (c) 2012-2032 Accounting Center of China Aviation(ACCA).
 * All Rights Reserved.
 */
package com.acca.action;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSessions;
import org.directwebremoting.ServerContextFactory;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * 
 *
 * @author zhouhua, 2012-11-25
 */
public class PublishAction extends ActionSupport {

    private String msg;

    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * @param msg the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public String showMsg(){
      HttpServletResponse response= ServletActionContext.getResponse();
      response.setContentType("text/html;charset=utf8");
      String page = ServerContextFactory.get().getContextPath() + "/message.jsp";
      Browser.withPage(page, new Runnable() {
          public void run() {
              ScriptSessions.addScript("showMsg('" + msg + "')");
          }
      });
    try {
        response.getWriter().print(msg);
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(msg);
        return SUCCESS;
    }

}
?
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;">
<title>Insert title here</title>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
	$(function() {
		dwr.engine.setActiveReverseAjax(true);
	});

	function showMsg(msg) {
        alert(msg);
	}

</script>

</head>
<body>
</body>
</html>
?
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;">
<title>Insert title here</title>
</head>
<body>
    <form action="publishAction.action" method="post">
      <input type="text" name="msg">
      <input type="submit" value="send">
    </form> 
</body>
</html>
?
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <welcome-file-list>
    <welcome-file>send.jsp</welcome-file>
  </welcome-file-list>
  
    <listener>
    <listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

    <!-- This should NEVER be present in live -->
    <init-param>
      <param-name>debug</param-name>
      <param-value>true</param-value>
    </init-param>

    <!-- Remove this unless you want to use active reverse ajax -->
    <init-param>
      <param-name>activeReverseAjaxEnabled</param-name>
      <param-value>true</param-value>
    </init-param>
    
    <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>

    <!-- By default DWR creates application scope objects when they are first
    used. This creates them when the app-server is started -->
    <init-param>
      <param-name>initApplicationScopeCreatorsAtStartup</param-name>
      <param-value>true</param-value>
    </init-param>

    <!-- WARNING: allowing JSON-RPC connections bypasses much of the security
    protection that DWR gives you. Take this out if security is important -->
    <init-param>
      <param-name>jsonRpcEnabled</param-name>
      <param-value>true</param-value>
    </init-param>

    <!-- WARNING: allowing JSONP connections bypasses much of the security
    protection that DWR gives you. Take this out if security is important -->
    <init-param>
      <param-name>jsonpEnabled</param-name>
      <param-value>true</param-value>
    </init-param>

    <!-- data: URLs are good for small images, but are slower, and could OOM for
    larger images. Leave this out (or keep 'false') for anything but small images -->
    <init-param>
      <param-name>preferDataUrlSchema</param-name>
      <param-value>false</param-value>
    </init-param>

    <!-- This enables full streaming mode. It's probably better to leave this
    out if you are running across the Internet -->
    <init-param>
      <param-name>maxWaitAfterWrite</param-name>
      <param-value>-1</param-value>
    </init-param>

    <!--
    For more information on these parameters, see:
    - http://getahead.org/dwr/server/servlet
    - http://getahead.org/dwr/reverse-ajax/configuration
    -->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>
  
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
?
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
  <package name="struts" extends="struts-default" namespace="/">
   <action name="publishAction" class="com.acca.action.PublishAction" method="showMsg">
     <result>message.jsp</result>
   </action>
  </package>
</struts>    
?
1.消息接收页面必须引入:
<script type="text/javascript" src="dwr/engine.js"></script>
2.所需jar包,因为本示例是结合struts2实现,所需jar包如下:
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
dwr.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
3.运行示例:
1)将示例部署到服务器上,启动服务器,访问http://localhost:8080/demo_dwr进入发送页面;
2)然后在新建一个页面,访问http://localhost:8080/demo_dwr/message.jsp进入接收页面,是接收页面保持打开状态
3)发送页面输入要发送的公告或消息,接收页面会弹出相应的信息
1 楼 jhacker7 20 分钟前  
之前用DWR的reverse Ajax做了个IM, 原理和你这个差不错,  需要处理好script session, 现在有了webscoket, 这种山寨的技术过时了, 哈哈
  相关解决方案