Updating Web Content Dynamically with Java (cont.)

Component Communication
On the server, an instance of the class "ImageApplication.java" accepts socket connections and spins off a new thread for each new connection request. To keep the code simple, each thread checks whether the data file has been modified. If the data file has been modified the thread reads the file and sends the new data to the connected applet. The sample application sends the entire file to the applet, but contains commented logic for sending only the changed data.

On the client, a hidden frame contains the "ImageApplet.java" applet so the applet tag is not visible from the browser source. The applet contains the logic to connect to the server from which it was loaded from and implement a simple communication protocol. After establishing the connection, it receives the data from the server, builds the HTML and invokes the javascript methods to pass the data to the document.

   public void upDateHTML(String str){
      //data is name of the form and quote is a 
      // JavaScript variable, 
      // str -- newly constructed HTML
      mainwin.eval("document.data.quote.value='" 
        + str + "'");
      mainwin.eval("javascript:assignData()");
      return;
   }
The netscape.javascript.JSObject accomplishes the applet-to-script communication. You need different versions depending on the client browser. You can download the zipped class file java40.jar for Netscape. The JSObject class ships with Internet Explorer, but it's a little hard to find. Search your $windows$\Java\Packages directory for the zip file containing the JSObject class.

The server sends instances of the ImageArrayElement.java class to the applet serialized as strings via the toString() method. The server constructs each object from the data file, invokes its toString() method, concatenates the string representation of all the objects, and sends the resulting string. On the other end the applet parses the string and reconstructs the ImageArrayElement objects. I chose to send the data as one long string as it requires very little processing and lets the user know as soon as the data changed to make it close the real time; however one alternative would be to send the objects in a vector.

In a production application, you would insert the new data into the page transparently, but to clarify the example I chose to notify the user about the arrival of new content. The main benefit of the push technology approach is that the application server sends only changed data across the network, so the latency is minimal. As the applet does very little work (no user-interface (UI) part involved because the browser is used as the UI), the applet is very small and loads very quickly.


Donepudi S. Raghu Kumar is a Senior Systems developer for SEMPRA Energy Trading Corporation. He designs and develops corporate Web applications, and performs research to improve Web application efficiency. He has a Masters degree in Computer Science from LAMAR University, TX. He is a SUN certified Java developer and the author of many Java tips and techniques. He can be reached at: dsraghu@hotmail.com or raghud@sempratrading.com.


DHTML Updates the Page

Serving Dynamic Content

Serving Dynamic Content Push Technology Serves Content
DHTML Updates the Page Component Communication







 TALK BACK
Are you using push techniques for your site? Is the method you're using similar to the one shown in this article? Let us know at web.dhtml.scripting.
Click here to Join
Documentation for the class netscape.javascript.JSObject

How Java-to-Javascript Communication Works In Java Plug-In





 
Sponsored Links

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers

Copyright Information/Privacy Statement