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"%>




Insert title here







 
<%@ page language="java" contentType="text/html; charset=utf-8"%>




Insert title here


    
 


  
    send.jsp
  
  
    
    org.directwebremoting.servlet.DwrListener
  

  
    dwr-invoker
    org.directwebremoting.servlet.DwrServlet

    
    
      debug
      true
    

    
    
      activeReverseAjaxEnabled
      true
    
    
    
           crossDomainSessionSecurity
           false
    
    
          allowScriptTagRemoting
          true
    

    
    
      initApplicationScopeCreatorsAtStartup
      true
    

    
    
      jsonRpcEnabled
      true
    

    
    
      jsonpEnabled
      true
    

    
    
      preferDataUrlSchema
      false
    

    
    
      maxWaitAfterWrite
      -1
    

    
    1
  

  
    dwr-invoker
    /dwr/*
  
  
  
    struts2
    
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	
  
  
    struts2
    /*
  
 



  
   
     message.jsp
   
  
    
 
1.消息接收页面必须引入:

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)发送页面输入要发送的公告或消息,接收页面会弹出相应的信息
  • demo_dwr.zip (4.2 MB)
  • 下载次数: 77

你可能感兴趣的:(DWR,Struts,推送)