inicio mail me! sindicaci;ón

With push send technical trends to update page content

One, what is dynamic content

Website of most popular Web gains profit from online ad business. The ad space in Web page is finite, be worth somewhat to let advertisement invest content, advertiser must enter much information in a place of strategic importance inside very little ad space not only, and the attention that makes sure advertisement can attract an user even. On current most website, the banner ad that puts Web page is constructing by the server commonly page while generate, we cannot insert new ad dynamicly had dispatched the page that go out. If want to show new ad, only way is new break page. We can use the methodological refresh page of process designing, for example:

The SetTimeOut function of object of use browser window is fixed ground refresh page. But, when using ad of refresh of this kind of method, the user can feel page refresh process apparently; In the meantime, want to decide an appropriate refresh frequency is very difficult also.

the page expire time of second of time setting amount to, make whenever input focus turns to this page (namely the browser is activationed) when, the browser can download this page afresh.

A few large websites, be like Yahoo.com and Msn.com, had used these technologies. These two kinds of methods have respective advantage and drawback. Below the case that uses Java only, we can pass network process designing and job of process designing of a few interfaces completely to implement system of banner advertising refresh, but must solve download time to grow the problem with refresh defer too.

2, with Java implementation content is pushed send

The communication between combinative Javascript frame manages the Java Applet that the network corresponds with, we can be used push send a technology to solve this problem. In a such systems, the task of Java Applet is to join the server monitors content to update. Once received new content, applet gives the HTML code that displays these new content with respect to construction, call function of a JavaScript to deliver the HTML that contains new content to JavaScript function. JavaScript function uses DHTML and DOM technology, the content that labels a <div> in the page is replaced with the new content that passes into in parameter. As a result of the limitation of browser safety, the Socket port that Applet place opens can receive the server that downloads this Applet repeatedly only.

Web server monitors join request in 80 port only. Accordingly, besides Web server, the network application that we still need a Socket connection that accepts Applet to request serves. Service of this network application inquires a database timely, alter data is released (push send) to all already connective Applet. Because handled function of the communication between the frame that conceals frame and JavaScript, we can from most JavaScript logic is concealed before the user.

In this whole processing process, the most difficult task is the communication between Java Applet and JavaScript code. Netscape offerred to call Netscape.javascript.JSObject kind. Want to use this target, join an Applet number that includes special “MAYSCRIPT” property please:

<APPLET Code= "MyApplet.class" Height=1 Width=1 MAYSCRIPT>

The method of JSObject allows Applet and documentation object alternant and call JavaScript command. For example, put the code below into Applet, we can visit window boy or girl friend:

Import Netscape.javascript. *; Public Class MyApplet Extends Java.applet.Applet{Private JSObject Mainwin; Public Void Init(){Mainwin = JSObject.getWindow(this); } }

After obtaining JSObject to cite, we can visit documentation window boy or girl friend, the Eval() method that adopts JSObject transfers JavaScript function.

3, update a page with DHTML

When keeping the new content that comes from Applet into documentation, to do not affect the content that has existed so, we can use the <div></div> number of HTML. This mark is different in IE and Netscape.

To IE and Netscape 6, this HTML mark is:

/ / the content that all wanting that update must use Id label <div Id= "iexplorer" Width=700px ></div>

To Netscape 4.x version, this HTML mark is:

<DATA><layer Id= "netscapev" ></layer></DATA>

Although we can be passed,cite proper ID, update HTML content directly from Applet, but to be clear about for the purpose of, we will put the program logic that updates HTML code into JavaScript function. The JavaScript code below saves the type of the browser to Ie variable:

Applnname=navigator.appName; If(applnname==”Microsoft Internet Explorer” ) {Ie=true; } Else {Ie=false; }

Applet anew data construction gives HTML code, save it to JavaScript variable Content, call AssignData() method next. Content data can be arrive to XML from pure HTML the any of binary data.

/ / call appropriate methodological Function AssignData() {If(ie) {explore();} Else {navig(); } } according to browser type

If the browser is IE or Netscape 6, applet calls Explore() method:

/ / Content is variable of a Javascript, it described need with HTML format / / indication new data Function Explore() {Iexplorer.innerHTML=content; }

If the browser is Netscape 4.0 taller perhaps version, applet calls Navig() method:

Function Navig() {Document.netscapev.document.write(“<DATA>“ Content “</DATA>“); Document.netscapev.document.close(); }

4, communication process

In server end, an ImageAppliation.java kind example answers Socket join plea, request to found a new line Cheng for each new link. To simplify code, each line Cheng checks data file to whether be changed only. If data file has been changed, criterion line Cheng is read take file content, transmit new data already connective Applet (application of give typical examples sends whole document to Applet) .

In client end, one concealed frame to include ImageApplet.java this Applet, examining function of HTML source code because of what this uses a browser cannot see Applet labels. Applet realized join server (the source server that downloads this Applet) function, achieved a simple communication agreement. After establishing the link with the server, applet receives the data that comes from a server, construction gives HTML code, call JavaScript function to pass into data documentation:

Public Void UpDateHTML(String Str){//data is the name that expresses sheet, / / Quote is variable of a JavaScript / / Str is the HTML code Mainwin.eval("document.data.quote.value=“" Str "“" ); Mainwin.eval("javascript:a that new construction comes outSsignData()” ); Return; }

Netscape.javascript.JSObject finishs the Applet communication to JavaScript, the client of different version carries a browser to need different version. You can download those who get is Netscape offerring compress kind of file Java40.jar. IE has contained JSObject kind, but search a bit hard. You can search $windows$\Java\Packages catalog to search include JSObject kind ZIP file.

Server ImageArrayElement.java kind example adopts ToString() method serial change become string to send Applet. The server gives each targets from data file construction, call ToString() method, join gets representing the string of all objects, send result string finally. And in another end, applet is received and analytic this string, new construction gives each ImageArrayElement targets. The form that string grows with here transmits data, because this kind of method needs very simple processing procedure only,be, make user can in order to be close to real time the change that speed is informed data instantly; But, we also can use additionally one kind of method, send an object with the form of vector namely.

In the application that runs formally in, you should make the process that new data inserts current page transparent commonly. But in application of give typical examples, to make order moving process more intuitionistic, it will hint when new content arrives user.

Push sending a technology the mainest good point is the data that applied server alters those only sends a network, make thereby defer was decreased least. Because this Applet is responsible finished work is very little (do not involve user interface, this part job is in charge of by the browser) , applet volume is very so minor, lade rate is very rapid.

5, how to run article example

Want to check application of article give typical examples, how must mix of Web server on your machine JDK 1.7 or taller version.

Install a point:

Unlock ZIP compresses a file and install catalog of root of Web server acquiesce.

To IIS server, acquiescent root catalog is Inetput\wwwroot

The free service that takes to Jsdk2.1 place implement, acquiescent catalog is < installation catalog > \webpages

After unlock compresses a file, all files will install <Web server root > / Exp/ catalog.

Below a few code add acquiescent page. Server of avery kind of has his acquiescent page, the acquiescent page of IIS is “default.htm” , refer to Web server documentation to understand specific specification please:

<ul><li><a Href= "/exp/ImageMain.htm ">Java Based Dynamic Ad-Banner</a></li></ul>

The measure of moving application:

Open window of a DOS, enter list of < acquiescent Web > / Exp, executive “java ImageApplication” . The system will show “Server Started Listening At Port 6011” . The attention ensures Classpath environment variable pointed to current job catalog.

Server of the Web that start.

Open a browser to input the URL below: Http://localhost:8080. This URL will open the acquiescent page of Web server, it should have link of a “Java Based Dynamic Ad-Banner” . Clicked this link to start the application of give typical examples of the article.

Open “/exp/images.txt” file with Notepad, duplicate stickup group content, save a file. You can see the system shows window of a JavaScript hints content is updated instantly. Close JavaScript window, the page will show new content.

Download the whole code of article example from here please, 411 KBhttp://www.89cn.com/down/pushweb.zip

Bookmark:Digg Del.icio.us Reddit

Leave a Comment