The Study of Ajax and Other Interesting Web Technologies

Brennan Spies's blog

The minor release with major features, Java 6 Update 10 brought a host of improvements to the long-neglected Java applet. One of these improvements was better interoperability with JavaScript, which was delivered to the new Java plug-in in the form of LiveConnect technology. As Sun's entrant into the RIA race, JavaFX, comes closer to a 1.0 release, such interoperability is crucial to the success of this effort.

While it has always been possible (to a certain extent) to call between an applet and JavaScript running the same browser, LiveConnect vastly improves the technology by

Improving reliability. A number of bugs involving the stability of the browser when making JavaScript<-->Java calls have been fixed.

Making it more consistent across different browsers. Support for JavaScript interoperability was always a little better on Mozilla browsers, but now the functionality is more consistent across different browsers with the new plug-in.

Increasing the functionality. It's now possible to instantiate new Java objects from JavaScript, call methods on them, as well as reference third-party packages on all browsers.

Calling Java from JavaScript

The following code shows how to invoke methods on an applet through JavaScript:

Public fields in the Java class can also be called from JavaScript, as well as static fields and methods. Due to a new synthetic keyword, Packages, that is now attached to every applet, it is now possible to instantiate/call methods on classes that are not applets. For example, if we add a new class in the org.example package,

This opens up the intriguing possibility of accessing a rich set of Java APIs from client-side JavaScript. Take, for example, the ability to add SVG support in (ahem) the only major browser that does not support it*. With Apache Batik and the Java plug-in, full support of SVG in the browser--SVG JavaScript API and all--becomes a reality. (Batik 1.7, by the way, is one of the most compliant implementations of SVG. And SVG might just be a faster way into the web designer's heart than JavaFX, but I digress...)

Each time a call into an applet is made from JavaScript, it is handled by a worker thread that is associated with the applet (there is only one per applet).

Calling JavaScript from Java

All instances of JavaScript objects within Java code appear as instances of netscape.javascript.JSObject. There are essentially two ways of invoking JavaScript from Java:

Calling the static JSObject.getWindow(Applet arg) method with the applet object passed in as an argument. This method returns a JSObject that represents the DOM Window. This JSObject can in turn be used to call() JavaScript functions or eval() JavaScript code. The API for JSObject is here.

Add a method in your Java code that takes JSObject as a formal parameter. Communication can then be bootstrapped from the JavaScript side by calling this Java method and passing a JavaScript object as an argument. It's a little less convenient than option 1, but useful if some JavaScript code wants to delegate some work to the Java code.

Furthermore, you can actually modify the HTML DOM from the Java side using a common DOM API. For the purposes of compiling Java code written with references to JSObject, you would place the jre/lib/plugin.jar on your classpath. Note also that, since JavaScript is a single-threaded language, communication from Java to JavaScript is restricted to one thread at a time; so you should structure your code accordingly.

Other JVM Languages

Of course, the other interesting possibility is the idea of languages like Scala, JRuby, Groovy, Clojure, or Fan being able to run in the browser with support for JavaScript interaction. A truly sick mind (and you know who you are) might use the applet merely as a trojan horse in order to get his/her favorite language running in the browser. I'll leave it at that!

Sun's renewed interest in the applet--sparked in part by RIA and in no small way by the resurgence of alternative browsers--has opened a door many thought closed a long time ago. Interesting times, indeed.

Sun Microsystems has released Java 6 Update 10, the so-called "consumer JRE" because of its ability to be deployed in a lighter-weight footprint (the "Java Kernel" or the core JRE classes) and download other classes on the fly as needed. The move is considered a necessary precursor to Sun's push into the RIA (Rich Internet Applications) space, where the accumulated bulk of the JRE is considered a drawback to faster, easier end-user distribution. Many of the improvements in Java 6u10 also pave the way for the future release of JavaFX technologies.

The New Java Plug-In

The Java Kernel is the key to the "Next Generation Java Plug-In Technology" for web browsers, which makes its debut in Java 6 Update 10. The most significant improvement in the new Java plug-in is the ability to launch applets from Java Network Launch Protocol (JNLP) files. This allows Java applets to reuse some of the extensions which had been previously available only for JNLP/Java Web Start technology, including access to JavaFX runtime libraries, support for 3D graphics via OpenGL, persistent data storage, and local filesystem storage. Other improvements in the Next-Gen Plug-In include better Java to JavaScript interoperability, a unified deployment mechanism for JNLP and applets, multiple JRE version support, classloader caching, new parameter arguments to <applet>,<object>, and <embed> tags, as well as numerous small improvements to the reliability and usability of applets. A full list of the new features is here.

Java Quick Starter

Also new to Update 10 is the Java Quick Start service, which preloads portions of the JRE onto the system disk cache, substantially decreasing the startup time. It is turned on by default in Windows 2000 and XP systems, but turned off in Vista since Vista offers its own preloading mechanisms.

Nimbus Look and Feel

Update 10 also comes with a new Swing look and feel, Nimbus. Nimbus gives a much needed face-lift to some of the stock L&F's available in the base platform with a richer, more attractive set of widgets.

Other Items of Note

Patch in Place. Update 10 brings the ability to do a patch in place when updating the JRE/JDK, allowing the simple upgrading of your installation.

Direct3D Support. On Windows platforms, the Direct3D pipeline has been completely revamped, offering much-improved rendering of Swing or Java2D applications which rely on alpha compositing, gradients, arbitrary transformations, filtering and other more advanced 2D operations. This upgrade relies on hardware acceleration (video cards) as well as the DirectX 9c runtime.

A complete set of notes on the changes in Java 6 Update 10 is available here.

Anyone who has ever programmed (hopefully briefly) in some type of assembly language has gone to the bare metal in implementing loops, if statements, and other programming language features. When programming in assembly, these constructs use some form of J* jump instruction together with labels to accomplish the task. In higher-level languages, jump instructions are usually associated with the infamous and properly banned goto statements (which are unrestricted jumps), but restricted jumps are far more common: return statements, the throw keyword (for exceptions), and of course the standard loops, if statements, and the like.

Being mainly a Java programmer, I occasionally miss language features such as continuations and co-routines. I came upon just such a situation recently, and I wondered if it was possible to imitate a continuation in Java without needing to resort to more sophisticated techniques such as Javaflow or RIFE's own continuation library. What I found out was that I could, albeit in a limited way.

Yesterday, the official Google blog broke the news that, yes, Google is indeed developing its own browser called Google Chrome. Much of the motivation and thinking behind this move is described interesting enough in an online comic strip.

A new tooltip telling you which tab/page in Firefox has enabled Firebug.

John Resig, creator of jQuery and now lead on the Firebug team, gives an excellent overview on his blog. Resig points out the currently rather global nature of enabling certain Firebug features, the overhead of enabling them (especially Script, which slows down all JavaScript running in Firefox about 25%), and states that all of these issues are targets for the 1.3 release.