Communication with Java and JavaScript

zhaozj2021-02-16  51

Text / jeremiah talkar

trust

1. The idea of ​​chat demonstration procedures, together with this article is my own. All chat procedures I have ever seen are not completely Java-based html. My method is a good balance between these two methods.

2. I collected technical information between Java and JavaScript communication through a large number of articles after reading a large number of articles in the Netscape developer site.

3. I first read the Javascript Apostle on the netscape site, "unknown applet (Faceless Applet)" when reading Danny Goodman's article. So I used this very cut term for the trust of Danny.

give up

1. The techniques discussed in this article have been tested using Internet Explorer 5.0 and Netscape Navigator 4.7 on Windows 2000 Professional machines. In the best work I know, these technologies should run on any browser 4.0 or more, but I can't guarantee me, because I don't have time to test it with these versions.

2. Because JavaScript is the only language that is supported in both major browsers, all script code uses this language. I used an available commercial plugin on http://www.ncompasslabs.com, which allows Netscape Navigator to support VBScript, but when I go to their site to verify this information, this product has not been listed.

introduction

I have been engaged in modern COM for two years, and now is the technical leadership of our company's E-commerce product team. I understand that the interface design and in different environments are like Visual C , Visual Basic's importance is effective under scripting. The ability of interface-based programming is always established in my mind, and as a software engineer I tried to apply it to everything I involved.

In the past year, when the ASP script is used as a binder for these objects, the experience tells us that all business logic should be compressed within COM objects. Designing and developing typical infrastructure and business objects require a higher technical device, it is used to require the actual use of the same thing. The preferred environment for developing these objects (at least in my team) is Visual C / ATL / STL. Microsoft also encourages Visual Basic to become the development environment that these objects can be selected.

These objects are represented as "unknown" because they achieve many logic but except for user interfaces. Its performance level is not a sufficient customer is an incumbent customer (this customer has logic of the information and aggregation information from the end user). This kind of customer then uses this information to do something meaningful by using unknown objects. This whole idea is that when the business object changes the unvermissive performance level, the representative requirements of many customers are customized. The change in the performance level can also be implemented by using fewer empirical procedures.

Apply these same principles to your browser environment, when using a scripting language processing performance, logic of compression height development in the "unknown" binary module looks logically. The options for these modules on the Windows platform are the Java Applet and ActiveX Control / Server. The focus of this article is to complete this goal using Java Applet, because Applet is independent of browser, platform, and processor (mostly for most).

Short history of applet

Sun Microsystems is introduced into Java Applet in the luminous role in 1895. Applet immediately won the WEB world, because they added the ability to dynamically display the Web content in a browser that was originally a static HTML world. In the initial days, using Java Applet as the best way to increase dynamic content in the web page. Initially Microsoft tried to use their ActiveX Control technology against the Java Applet provided by Sun, but there are two main issues in the web page internal use:

● The binary module is specified by the processor, so it is not suitable as part of the web page. One of the main reason for World Wide Web is the fact that most of the webpages written in W3C standard HTML is unknown to browser and processors. The ActiveX control is not in line with this example.

● Safety is a big problem because the control writer has enough rights to access resources on the client machine. The named control allows anyone to view the webpage and smart to download a decision of a specific control on his / her machine, but all him is doing a button accidental click (or happily ignored), This will leave a client that is vulnerable, which is also in line with the intention of malicious control writers.

When the dynamic HTML finally started, things changed thoroughly. Document Object Model uses components that can be designed, which exposes web elements with their own properties and methods. Even if Internet Explorer and Netscape Navigator browser have many different dynamic HTMLs, there are many different dynamic HTMLs, but use script code programming changing display page content itself is a huge success. Applet suddenly begins to look old and rough. W3C has finally adjusted the new type of dynamic HTML, and the new type of dynamic web page is tone.

In the browser, you have the following advantages to using Java Applet:

● Applets (for most applets) can work on multiple browsers, platforms, and processors.

● Java language is a typical a powerful conceptual language.

● JDK has a number of useful classes that are typically created in the high-level library.

● The technology has been conceived, and the applet can only be run in the box with the default value. If they want to break the limit, the applet must be named.

● Applets can be contacted with web servers when they send back user information, upload / download files, and the like. The named Applet can be contacted with any server, not just a host.

● The Applet code cannot be seen through the "View Source File" option, so the intellectual property rights are protected.

● Java's .class file is very small, the result is very fast.

Disadvantages using Java Apple:

● In a browser conversation, download Applet is not a web page to use their first time display is that the web page is then refreshed after display. Applet does not have a long duties in the browser conversation. In fact, this can be considered as an advantage over most cases.

● Applet is spent for a long time initialization.

● Because Java's .Class file is byte code that is interpreted by Java Virtual Machine, JVM, the applet is slower than the local code.

● An applet is just part of the true state on the browser, which does not seamlessly appear in web content. Cascading Style Sheets, CSSs do not directly affect the rectangular area occupied by Applet.

● Netscape Navigator 4.x has ten active Applet limits. I don't know any such limit in Internet Explorer 4.0 . Quick review using Java Applets

Java Applets is included in an HTML web page by using the Applet tag. The 13.4 section of the HTML 4.01 manual on the W3C site illustrates this tag in detail. It also refers to , and does not agree with this tag.

An HTML page that simply contains Applet is as follows:

Calculator </ Title></p> <p></ hEAD></p> <p><body></p> <p><applet id = "Calculator" width = "300" Height = "500" code = "Calculator.class" codebase = "></p> <p><param name = "initialmode" value = "normal"></p> <p></ applet></p> <p></ body></p> <p></ html></p> <p>The properties used in the above example are explained below:</p> <p>Id</p> <p>Applet instance identifier. The customer script code can know the applet of this ID.</p> <p>Width</p> <p>This property specifies the initial width of the Applet display (excluding a window or dialog created by applet). Even if I successfully use the browser mentioned in the "Abandon" section, it is recommended to use the value 1 as the minimum possible width.</p> <p>HEIGHT</p> <p>This property specifies the initial height of the Applet display field (excluding a window or dialog created by applet). Just like the width properties, it is recommended to use the value 1 as the minimum possible width.</p> <p>Code</p> <p>This property specifies any type of file (including Applet subscript subclass or the path to the class, including the class file itself), which is explained when it comes to the applet's CodeBase.</p> <p>Codebase</p> <p>This attribute specifies the basic URI of the applet. If this property is not specified, then it defaults the most recent document as the same basic URI.</p> <p>Only Code, Width and Height properties are must be.</p> <p>The PARAM tag contains a value of a pair of names, which allows Applet to install yourself for the first time.</p> <p>The JavaScript function to call a method on the applet is as follows: it is very simple:</p> <p><script language = javaScript></p> <p>Function setCalculatorMode (MODE)</p> <p>{</p> <p>Document.calculator.SetcalculatorMode (Mode);</p> <p>// Alternative Way to Reference The applet.</p> <p>// Document.Applets [0] .SETCALCULATORMODE (MODE);</p> <p>}</p> <p></ script></p> <p>Labor division of labor</p> <p>In the introduction part of this article, I suggest that one method of highly developed browser FFC processing is compressed into unknown Java Applets when processing performance forms through JavaScript code. This method requires two-way communication between Java and JavaScript. The following chapters will study the options available.</p> <p>The members and functions exposed by JavaScript code Access Java Applet are straightforward, like the setcalculatorMode () function description in the previous section. The applet within the document is not by using its ID / NAME is an index that uses Applet collected.</p> <p>E.g:</p> <p>document.Calculator.SetCalculatorMode (Mode); // or document.applets [0] .SetCalculatorMode (Mode); netscape.javascript.JSObject using classes and class completed netscape.javascript.JSException other direction (Java to JavaScript) communication. In order to find these classes, I look for all files that contain string "jsobject" on my hard drive. It is surprising that these documents are widely used in many different programs, including part of the Visual InterDev engineering.</p> <p>If Netscape Navigator 4.0 is installed on your machine, these .class files are available in the java40.jar file under the <Navigator Installation Directory> / Communicator / Program / Java / Classes directory.</p> <p>I am also looking for these .class files in the four different .zip files in the <Windows Installation Directory> / Java / Packages directory. These .zip files are obviously the Microsoft product installation because they contain many com.ms packages. Their use is that these two classes are available to any browser, you can set your ClassPath environment variable to any of the above paths. An optional method is to use a program to unpack these files from .jar or .zip files to your applet directory.</p> <p>JSOBJECT class</p> <p>The short description of the JSObject class member function is to better understand the use of this class.</p> <p>Public Static JSObject getWindow (Applet Applet)</p> <p>This static method returns a JSObject on the Applet window containing the given. For example: jsobject mainwindow = jsobject.getwindow (this);</p> <p>Public Object Call (String MethodName, Object Args [])</p> <p>This function calls a JavaScript method from the Java Applet inside the Java Applet. E.g:</p> <p>JSObject mainwindow = jsobject.getwindow (this);</p> <p>String arguments [] = {"90", "2"}; // {"Percent Complete", "Time Remaining"}</p> <p>Mainwindow.call ("UpdateProgressindicator", arguments;</p> <p>Public Object Eval (String S)</p> <p>This method finds a value of a JavaScript expression. The expression is a string of the JavaScript source code that is treated in this object context. E.g:</p> <p>JSObject mainwindow = jsobject.getwindow (this);</p> <p>JSObject username = mainwin.eval ("document.userinfoform.username);</p> <p>Public Object getMember (String name)</p> <p>This method retrieves an index member of the JavaScript object, equivalent to this.name of the JavaScript object. :</p> <p>JSObject mainwindow = jsobject.getwindow (this);</p> <p>JSObject DocumentPage = (JSObject) Mainwindow.getMember ("Document"); jsobject userinfoform = (jsobject) DocumentPage.getMember ("UserInfoForm");</p> <p>JSObject username = (jsobject) UserInfoform.getMember ("UserName");</p> <p>Public Object GetSlot (INT INDEX)</p> <p>This method retrieves an index member of the JavaScript object, equivalent to the JavaScript object of this [Index]. E.g:</p> <p>JSObject mainwindow = jsobject.getwindow (this);</p> <p>JSObject DocumentPage = (JSObject) Mainwindow.getMember ("Document");</p> <p>JSOBJECT Applets = (JSObject) DocumentPage.getMember ("Applets");</p> <p>Object theapplet = Applets.getslot (Index);</p> <p>Public Void Removemember (String Name)</p> <p>This method deletes a designated member of a JavaScript object.</p> <p>Public void setmember (String name, Object Value)</p> <p>This method sets a designated member of a JavaScript object. It is equivalent to this.name = value of the JavaScript object. E.g:</p> <p>JSObject mainwin = jsobject.getwindow (this);</p> <p>JSOBJECT DocumentPage = (jsobject) mainwin.getmember ("document");</p> <p>JSObject UserInfoform = (jsobject) DocumentPage.getMember ("UserInfoForm");</p> <p>JSObject username = (jsobject) UserInfoform.getMember ("UserName");</p> <p>UserName.setmember ("Value", "Jeremiah S. Talkar");</p> <p>Public void setslot (int index, object value)</p> <p>This method sets an index member of a JavaScript object. It is equivalent to this [index] = value of the JavaScript object.</p> <p>Public String Tostring ()</p> <p>This method converts JSObject into a string.</p> <p>The above example is very clear. The public approach to the JSObject class is not limited when tuning the JavaScript function in the JavaApplet. They can also cause an applet to process document object model elements directly.</p> <p>These complete documents can be obtained on http://developer.netscape.com/docs/manuals/communicator/jsref/pkg.htm.</p> <p>The document also explains how to process data types between Java and JavaScript.</p> <p>The meaning of the MayScript property</p> <p>Even if the applet calls the JavaScript function, or directly accesses the document object model, if the applet tag does not contain the method of jsobject in the MayScript property, the JSObject method will fail. This can make web designers to measure whether an applet can evoke JavaScript.</p> <p>Unknown APPLETs are unknown in the webpage, but when you use the Java Applet, an applet requires direct communication with another applet. Such a call can also communicate through a media JavaScript function, but familiar with all available options is always better.</p> <p>The AppletContext interface of the Java.applet package implements an access restriction on the context of the applet, like a browser inserted into the applet, and an applet has other Applets on the web page is also on the same web page.</p> <p>For example, the following is an HTML page contains two applets:</p> <p>HTML> <head> <title> Communication Between Applets </ title> </ head> <body> <applet code = "circlearea.class" name = "circlearea" width = 1 height = 1> </ applet> <applet code = "Picalculator.class" name = "picalculator" width = 1 height = 1> </ applet> ... </ body> </ html></p> <p>The following code indicates the usage of AppletContext objects:</p> <p>AppletContext Context = getAppletContext (); picalculator piapplet = (picalculator) Context.getApplet ("picalculator"); piapplet.getValueOfpi ();</p> <p>Another option is to use the AppletContext :: getApplets () method, which returns an enumeration type of all applets in the document. Even if communication between Applet is part of the standard Java Applet API, it will not be supported in all browsers that allow Java. The core coding of other applets is also unswerving. The best way can use the JavaScript function to handle this communication.</p> <p>Java's iUnknown :: QueryInterface class object is the root of the class level. Each class has a superclass. All objects, including arrays, implementing this class. Object :: getClass () Method Returns "class" with many useful functions that explore the Java class itself. Although in the previous section of this article has been explained in detail, I still want to point out the GetInterfaces () method to make the dynamic interface discovery through a Java class.</p> <p>I have not tried to call the getClass () method in JavaScript, so it cannot comment on it. But using the basic type applet, this functionality can easily make it available for scripting code.</p> <p>Install sample file</p> <p>The sample code for this article has been compressed in the Java2JavaScript.zip file. The sample file demonstrates a chat program that has been simply sending messages in the client side. In this chat on a different machine, a message is sent to the server and then broadcasts to all participants.</p> <p>The file that makes up the example is:</p> <p>ISession.java Defines the source file of the iSession interface iSession.class iSession interface Java byte code ChatClient.java Demo the source file of the Chatclient Applet Chatclient.class Chatclient Applet class Java byte code CompileChatClient.bat Simple Java source file batch Document TestChatClient.htm Supports the ChatClient Applet HTML file JSObject.class JSObject class byte code JSException.class JSException class's Byte Code Installation and Run Sample Program The steps of:</p> <p>● Unpacking Java2JavaScript.zip to the directory you selected on the machine running with Personal Web Server or Internet Information Server.</p> <p>● Make sure that JSObject and JSException class files in the Netscape / JavaScript subdirectory in the installation directory are available.</p> <p>● Right-click the installation directory to select "Properties".</p> <p>● Click the "Web Sharing" tab, select the "Sharing this folder" radio button, accept the default value "virtual directory" in the pop-up dialog.</p> <p>● Finally, start Internet Explorer 4.0 or ​​Netscape Navigator 4.0 programs, then enter the URL http: // <machine name> / <virtual directory> /testchatclient.htm.</p> <p>Enter some text in the input box, then click the appropriate "Send" button, you will see the message appears in the chat window.</p> <p>Sample code explanation</p> <p>The chat program is a popular cooperative mechanism on the web page. There are two types of chat programs I have ever seen:</p> <p>● One is a Java Applet that manages user interfaces and servers.</p> <p>● The other is an HTML web page for all new messages after the last refresh is displayed every few seconds.</p> <p>I have to realize product quality for the chat procedures of our E-commerce products. After many considerations, I decided to use a mixed method. First of all, Java Applet is an unknown applet that executes an ISession interface.</p> <p>Public Interface ISession</p> <p>{</p> <p>// Type is buy to Differentiate The Actual Message String</p> <p>// and can be set to 'text', HyperLink 'etc.</p> <p>// Should Be Invoked First To Indicate To The Server That A</p> <p>// New Person Has Joined The Chat.</p> <p>Public int start (String Strauthor, String String String Strtype, StrMessage);</p> <p>// Should Be Invoked When The Author Wants to EXIT The CHAT.</p> <p>Public int endsession (String Strauthor, String Strtype, String StrMessage);</p> <p>// use to send the chat.</p> <p>Public int desundMessage (String Strauthor, String Strtype, String StrMessage);</p> <p>}</p> <p>I have made this interface in the chat program product version slightly modified, including the Author parameter of EndSession () and SendMessage (). This is because my demo uses an applet to deliver chat messages between two different chatters.</p> <p>The chatclient.java file is the actual execution file of the iSession interface. If they are executed, the browser calls the function init (), start (), and STOP (). The JSObject describing the document window is obtained after the init () method is executed. // Get The JavaScript Window That Will Have The Various Scripts That This Applet Will Call. M_jscriptwin = jsobject.getWindow (this);</p> <p>Because the Java Applet calls two different JavaScript functions, I decided to specify the name of these functions like Applet's parameters until the same applet provides the default value.</p> <p>m_StrMessageHandler = getParameter ("MessageHandler");</p> <p>m_strerrorhandler = getParameter ("ErrorHandler");</p> <p>BegInSession () and endsession () are virtual execution functions that only perform sendMessage ().</p> <p>SendMessage () Calls the JavaScript function name specified in the M_StrMessageHandler member variable. The default is "HandlesessionMessage". The relevant code is as follows:</p> <p>IF (m_jscriptwin! = NULL)</p> <p>{</p> <p>String arguments [] = {Strauthor, Strtype, StrMessage};</p> <p>m_jscriptwin.call (m_StrMessageHandler, Arguments);</p> <p>}</p> <p>HandlesessionError () calls a JavaScript function name specified in the M_STRRORHANDLER member variable. The default is "HandlesessionError".</p> <p>TestChatClient.htm file processing chat performance. Applet uses the <Applet> tag to be included in the web page.</p> <p><applet ID = "chatapplet" width = "1" Height = "1" code = "chatclient.class" codebase = "." Viewastext MayScript></p> <p><param name = "messageagehandler" value = "handleasessionMessageex"></p> <p><param name = "errorhandler" value = "HandlesessionerRROREX"></p> <p></ applet></p> <p>The parameter specifies the name of the two JavaScript functions of the applet call. I only indicate that the name except the default, this method is how flexible, two forms in the web page simulates two people chatting with each other. Related HTML is also easy to understand.</p> <p>In fact, the message itself is displayed on <DIV>. In Internet Explorer, I use the table object model to display each message in a single row. So ChatMessageStable is defined on <div>.</p> <p>Finally, the HandlesessionMessagesex () JavaScript function processes all performance. In Internet Explorer, each message is issued after the ChatMessageStable table adds a new line. The scroll bar will also be displayed if needed. In Netscape Navigator, I add new messages to the Messages variable and use the latter update Chatmentages <DIV>. Since the scroll bar does not automatically display on Netscape <Div> (a real layer), then I display the last received message at the top. I have found an article that supports the Layer scroll bar in Navigator, but has nothing to do with this example. Last thoughts</p> <p>This article strives to introduce some of the browser logic (elegant) technology. As I mentioned in this article, JSOBJECT is widely used in many programs, including Microsoft. Similarly, you need to consider your personal environment to determine if the technology introduced here is available.</p> <p>As for the sample program attached to this article, I think the introduction of JavaScript / DHTML to achieve chat makes this code to maintain through the entry level / low-level programmer. The customization of the user interface is also easy to use DHTML / JavaScript. In addition, it allows you to look at the remaining web content, and more powerful performance technology.</p> <p>In this program's product version, I increased the support of the open-opened super connection on the participant machine, using a stacked style single dynamic selection message color, and the like.</p> <p>Welcome any feedback!</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-27773.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="27773" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.047</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'vz0dhyk_2FimQ2W8KdciqSpjv7icZfiKdl_2F6FpTRPUx_2B0ZsGcK8QGzKNnDov2oIZjXqZznF8aFqz7C9uWhHnXRRQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>