Applets are back: new concepts, new design, new features
A Java applet is an applet delivered in the form of Java bytecode. Java applets can run in a Web browser using a Java Virtual Machine (JVM), or in Sun’s AppletViewer, a stand-alone tool for testing applets. Java applets were introduced in the first version of the Java language in 1995. Java applets are usually written in the Java programming language but they can also be written in other languages that compile to Java bytecode such as Jython.
Applets are used to provide interactive features to web applications that cannot be provided by HTML. Since Java’s bytecode is platform independent, Java applets can be executed by browsers for many platforms, including Windows, Unix, Mac OS and Linux.There are open source tools like applet2app which can be used to convert an applet to a stand alone Java application/windows executable/linux executable. This has the advantage of running a Java applet in offline mode without the need for internet browser software.
A Java Servlet is sometimes informally compared to be “like” a server-side applet, but it is different in its language, functions, and in each of the characteristics described here about applets.
Life Cycle of an old Applet: Basically, there are four methods in the
Applet class on which any applet is built.
- init: This method is intended for whatever initialization is needed for your applet. It is called after the param attributes of the applet tag.
- start: This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages.
- stop: This method is automatically called whenever the user moves away from the page containing applets. You can use this method to stop an animation.
- destroy: This method is only called when the browser shuts down normally.Thus, the applet can be initialized once and only once, started and stopped one or more times in its life, and destroyed once and only once.
Applets are back!
The Next-Generation Java Plug-in Technology runs applets in a different, more efficient and more reliable way than ever before. Now you can reap the following benefits:
- Improved reliability
- Improved user experience
- Applets launch in the background
- Built-in JNLP support
- Per-applet command line arguments
- Heap size, Java 2D API acceleration options
- Improved Windows Vista support
- Signed applets now work correctly in Protected Mode Internet Explorer
The next-generation Java Plug-in offers a completely redesigned architecture, and is available in the Java SE 6 Update 10. This plug-in provides powerful new capabilities to applets in the web browser, while improving the overall reliability and functionality of applets in a backward-compatible manner.
The most significant new feature of the next-generation Java Plug-in is built-in support for launching applets from JNLP files. Using the JNLP file format as the applet descriptor allows applets to instantly reuse JNLP extensions previously written for Java Web Start applications, and significantly expands the capabilities of applets in many other ways.
A New Way of Executing Applets
The new way of executing applets has architectural similarities to Java Web Start technology, but tighter browser integration. Applets no longer execute in a Java Virtual Machine (JVM) inside the web browser. Instead, a separate JVM machine process is launched to execute applets. By default, only one JVM machine is launched, but you have the opportunity to launch more than one JVM machine, and you get support per-applet command-line arguments, so you can affect heap size or other requests.
In Figure 1 above, the clouds represent JVM instances. There is a small, headless JVM inside the browser that is used to manage the connections to one or more client JVMs that actually run the applets. In the diagram the Dukes represent applets. One JVM instance is running two applets and the other is running one.
Applets launch directly from JNLP files, make use of the same descriptor used by Java Web Start software, and allow more powerful parameters than the classic “archive”, “code”, and “cache_archive” parameters.
The new plug-in provides:
- access to advanced JNLP extensions previously available only to Java Web Start software applications. A small set was previously available, with restrictions, and these restrictions have now been removed.
- access to the JNLP APIs from applets.
- PersistenceService, DownloadService.
- control over heap size, command-line arguments, JRE version selection, and auto-download. You have the same capabilities as Java Web Start software for applications.
Now, you use something like the following on the web page:
<applet width="500" height="500"> <param name="jnlp_href" value="my_applet.jnlp"> </applet>
Calls to the applet lifecycle methods
destroy are more deterministic, and cross-browser behavior has been improved. The applet class loader cache and the legacy applet lifecycle, required for backward compatibility, are fully supported and the behavior of both has been improved.
The applet behaves exactly like an application started with Java Web Start software.
jnlp_href parameter bridges between the web page and the JNLP description of the applet. Applet tag and JNLP file have overlapping mechanisms for specifying things like the width and height.
In general you should use the Deployment Toolkit, also new in Java SE 6 Update 10, to automatically generate the HTML for the applet tag. The deployment advice guide shows how to easily and portably deploy applets using the Deployment Toolkit. (more)