Dynamic update page content with push technology
(Panasonic July 06, 2001 21:15) One, what is dynamic content Most popular Web websites profit from online advertising business. The advertising space in the web page is limited. In order to make advertising investments value, advertisers must not only plug in a large amount of information in small advertising space, but also guarantee that advertisements can attract users' attention. On most websites, the banner ads placed in the web page is usually generated by the server while constructing the page, we cannot dynamically insert the new ads into the page that has been sent. If you want to display new ads, the only way is to re-refresh the page. We can refresh the page with programming methods, for example:
Use the SETTIMEOUT function of the browser window object to periodically refresh the page. However, when refreshed with this method, users will significantly feel the page refresh process; at the same time, it is difficult to determine a suitable refresh frequency. Set the expiration time of the page to several seconds, so that when you enter the focus to transfer to the page (ie the browser is activated), the browser will re-download the page. Some large websites, such as Yahoo.com and msn.com have already adopted these technologies. Both methods have their own advantages and disadvantages. In the case of only Java, we can implement banner ads through network programming and some interface programming, but you must solve problems with too long and refresh delays in download time. Second, use Java to implement content to push Java Applets in conjunction with JavaScript inter-frame communication and a management network communication, we can use push technology to solve this problem. In such a system, the task of the Java Applet is to connect the server and listen to the content update. Once the new content is received, the applet constructs an HTML code that displays these new content, calls a JavaScript function and passes the HTML containing the new content to the JavaScript function. The JavaScript function uses DHTML and DOM technology to replace the new content incorporated in the page in the page in the parameter in the page. Due to the security of the browser, the Socket port opened by the Applet can only be connected to the server downloading the applet. The web server listens for the connection request at the 80-port. Therefore, in addition to the web server, we also need a web application service that accepts the Applet's Socket connection request. This web application service is queried to query the database, publish the change data (push) to all the connected Applet. Due to the use of hidden frames and JavaScript frame communication features, we can hide most JavaScript logic from the user. During this entire process, the most difficult task is communication between Java Applets and JavaScript code. Netscape provides a class called Netscape.javaScript.jsbobject. To use this object, join an Applet tag that contains a special "MayScript" property:
import netscape.javascript *;. public class MyApplet extends java.applet.Applet {private JSObject mainwin; public void init () {mainwin = JSObject.getWindow (this);}} After obtaining JSObject references, we will be able to access the document window object And invoke the JavaScript function through the eval () method of JSObject. Third, when using the DHTML update page, when writing new content from the applet, in order not to affect the original existing content, we can use HTML
Applnname = navigator.appname; if (Applnname == "Microsoft Internet Explorer") {IE = true;} Else {IE = false;} Applet constructs HTML code from the new data, saved it to the JavaScript variable content, and then call AssignData. )method. The content data can be anything from pure HTML to XML to binary data.
/ / Call the appropriate method according to the browser type Function AssignData () {if (ie) {explore ();}}} If the browser is IE or Netscape 6, Applet calls the expect () method:
// Content is a JavaScript variable that describes the new data required for // display in HTML format () {iXplorer.innerHtml = Content;} If the browser is Netscape 4.0 or higher, Applet calls Navig () method :
Function navig () {document.netscapev.document.write (''); document.netscapev.document.close ();} four, communication process is on server side, one imageAppliation The instance of the .java class responds to the Socket connection request and creates a new thread for each new connection request. To simplify the code, each thread only checks if the data file changes. If the data file has changed, the thread reads the file content and sends the new data to the connected Applet (sample application sends the entire file to the applet). On the client, a hidden frame contains imageApplet.java this applet, so the browser's viewing HTML source code function is unable to see the applet tag. Applet implements the functionality of the connection server (download the source server) of the applet, and implements a simple communication protocol. After establishing the connection with the server, the applet receives data from the server, constructs HTML code, and calls the JavaScript function to incorporate the data into the document: public void updateHTML (String str) {// Data is the name of the form, // quote is a form JavaScript variable // Str is a newly constructed HTML code mainwin.eval ("Document.Data.quote.Value = ' STR "'); mainwin.eval ("JavaScript: AssignData ()"); Return; } Netscape.javaScript.jsObject Completes the communication to JavaScript, and different versions of client browsers require different versions. You can download the compressed class file java40.jar provided for Netscape. IE already has a JSOBJECT class, but it is a bit hard to find. You can search for $ Windows $ / java / packages directory looking for ZIP files with the JSObject class. The server puts an instance of the ImageArrayElement.java class through the toString () method to serve as a string to the applet. The server constructs individual objects from the data file, call the toString () method, and the connection is obtained to represent the string of all objects, and finally send the result string. At the other end, the applet receives and parses this string, and the respective ImageArrayEleMent objects are re-constructed. This is used to send data in the form of a long string because this method only requires a very simple processing process, so that the user can immediately know the changes in the data at a time close to real-time; however, we can also use another one. The method is transmitted in the form of a vector. In a formal running application, you should generally let new data insert into the current page process transparent. However, in the example application, in order to make the program run more intuitive, it will prompt the user when the new content arrives. The most important advantage of pushing technology is that the application server transmits only those changes to the network, making the delay to minimize. Since this applet is very working in the work (not involving the user interface, this part of the work is responsible by the browser), so the applet is very small and the loading speed is very fast. 5. How to run this article instance To test this document application, you must have a web server and JDK 1.7 or higher on your machine. Installation points:
Unlock the ZIP compressed file and install it to the web server default root directory. For IIS servers, the default root directory is INETPUT / WWROOT for free servers with JSDK2.1, and the default directory is the
Open a DOS window, enter the
Documentation for the class netscape.javascript.jsobject How Java-To-JavaScript Communication Works Netscape Internet Explorer Please download the full code of this document, 411 KB. Responsible Editor: Little Li (lisz@staff.ccidnet.com)