pushlets技术-开发日记1

Pushlets:从Servlet发送消息给DHTML客户端浏览器- -
Tagopen    source                                          

 

Pushlets:从Servlet发送消息给DHTML客户端浏览器

 

来自RMI或者CORBA的服务器端回掉可以通知客户端服务器上有什么变化。但是如果客户端是浏览器,情况又如何呢?本文介绍pushlets来完成此功能。

 

 

 

 

 

servletjsphtmlapplet 

1, server-to-web client  

pushletserver-to-web clientHTMLserver-side callbackmessaging 

HTML refresh

 

<META HTTP-EQUIV="Refresh" CONTENT="4;URL=http://www.justobjects.nl">

 

 

Server-side callback

 

server-side callbackserverjava-appletRMICORBARMICORBAserverserverjavaworldhttp://www.javaworld.com/javaworld/javaqa/1999-04/05-rmicallback.html 

Messaging

 

appletmessageing serverTCP/IPUDPmulticastSoftWirediBusIBMMQSeriesBEAJMS 

Pushlet 

PushletappletHTMLHTTPservlet 

Pushlet  

HTTP streaming

PushletHTTP streaminghtmlHTTP 

JSPservletHTML  

<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
</HEAD>
<BODY BGCOLOR="blue" TEXT="white">
<%
 int i = 1;

 

 try {
    while (true) {
       out.print("<h1>"+(i++)+"</h1>");
       out.flush();

 

       try {
            Thread.sleep(3000);
       } catch (InterruptedException e) {
       out.print("<h1>"+e+"</h1>");
        }
     }
   } catch (Exception e) {
       out.print("<h1>"+e+"</h1>");
   }
%>
</BODY>
</HTML>

 

 

pushlet3push-js-stream.htmlpush-js-stram-pusher.jsp,push-js-stream-display.htmlpush-js-stream.html push-js-stram-pusher.jsp
<%
   /** Start a line of JavaScript with a function call to parent frame. */
   String jsFunPre = "<script language=JavaScript >parent.push('";

 

   /** End the line of JavaScript */
   String jsFunPost = "')</script> ";

 

   int i = 1;
   try {

 

     // Every three seconds a line of JavaScript is pushed to the client
     while (true) {

 

        // Push a line of JavaScript to the client
        out.print(jsFunPre+"Page "+(i++)+jsFunPost);
        out.flush();

 

        // Sleep three secs
        try {
             Thread.sleep(3000);
        } catch (InterruptedException e) {
             // Let client display exception
             out.print(jsFunPre+"InterruptedException: "+e+jsFunPost);
        }
      }
    } catch (Exception e) {
             // Let client display exception
             out.print(jsFunPre+"Exception: "+e+jsFunPost);
    }
 %>
Push-js-stram.html
<script LANGUAGE="JavaScript">
var pageStart="<HTML><HEAD></HEAD><BODY BGCOLOR=blue TEXT=white><H2>Server pushes: <P>";
var pageEnd="</H2></BODY></HTML>";

 

 // Callback function with message from server.
 // This function is called from within the hidden JSP pushlet frame
 function push(content) {

 

    // Refresh the display frame with the content received
    window.frames['displayFrame'].document.writeln(pageStart+content+pageEnd);
    window.frames['displayFrame'].document.close();
 }

 

</script>
</HEAD>

 

<FRAMESET BORDER=0 COLS="*,0">
     <!-- frame to display the content pushed by the pushlet -->
     <FRAME SRC="push-js-stream-display.html" NAME="displayFrame" BORDER=0 SCROLLING=no>

 

     <!-- Hidden frame with the pushlet that pushes lines of JavaScript-->
     <FRAME SRC="push-js-stream-pusher.jsp" NAME="pushletFrame" BORDER=0 SCROLLING=no>
</FRAMESET>

 

Push-js-display.html
<HTML>
<BODY BGCOLOR=black TEXT=white>
<H1>WAIT...</H1>
</BODY>
</HTML>

 

pushletservletjspJavascriptframe 

 

Design of the Framework

 

PushletPublish-SubscribeObserver 

l          Javapushlet.javaUMLl         (pushlet.js)html(pushlet.html)DHTML2.0.0libjs-pushlet-client.jspjs-pushlet-net.htmll         java(JavaPushletClient.javaJavaPushletClientListener.java)Javal         DHTML layerlayer.jslayer-grid.jslayer-region.jsl         EventGenerators.javatemperature.html

  

PushletservletPublisherSubscriberEventPushlet servletEvent

 

 

- 作者: dlstone 访问统计: 516 2005年07月5日, 星期二 09:23 加入博采

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值