Next-generation Java Applet plug-in technology

Source: Internet
Author: User

Java SE 6The Java Desktop Application is greatly upgraded and the Java SE 6 Update n plan is launched. This plan aims to simplify the size of JRE and improve the user's installation experience, A new applet browser plug-in is provided, which will be released with Java SE 6 Update 10. This article comprehensively introduces the key features of this new plug-in, and introduces the application of this plug-in using NASA's World Wind as an example. (Last updated in 2008.07.15)

The applet is back!

In order to pass your program on the network, it is time to consider againJava AppletTechnology. The next-generation Java Plug-in technology runs the applet in a different, more efficient and reliable way than in the past. Now you can get the following benefits:

Enhanced reliability

Improved User Experience

Start the applet in the background

Built-in JNLP support

Command line parameters for each Applet

Heap memory size, Java 2D API acceleration options

Improved Java/JavaScript programming language integration

Improved Windows Vista support

The signed applet can now run normally in the protection mode of Internet Explorer.

The next-generation Java Plug-In provides a completely redesigned architecture that will appear in Java SE 6 Update 10. The plug-in provides powerful new functions for the applet running in the web browser. It improves the reliability and functionality of the entire Applet in a backward compatible manner.

The most significant new feature of the next-generation Java Plug-in is its built-in support for starting the applet through the JNLP file. Using the JNLP file format as the applet descriptor allows the applet to immediately reuse the JNLP extension previously written for the Java Web Start application.

  New Way to execute Applet

The new way to execute the applet is similar in structure to Java Web Start technology, but more closely integrated with the browser. The applet does not run in the JVM in the web browser, but starts an independent JVM process to run the applet. By default, only one JVM will be started, but you can also start multiple JVMs, And you can set command line parameters for each applet, so you can influence the heap memory size or other requirements.

 

Figure 1. Applet Architecture

In, cloud indicates the JVM instance. There is a small headless JVM in the browser that is used to manage connections between one or more client JVMs. These JVMs run the applet. In this figure, Duke indicates the applet. One JVM instance runs two applets, And the other runs one applet.

The applet starts directly from the JNLP file. The JNLP file used by the applet is the same as the descriptor file used by the Java Web Start software, and the typical "ARCHIVE ", "Code" and "cache_archive" are more powerful parameters.

The new plug-in provides:

Access to advanced JNLP extensions dedicated to Java Web Start Software. A small number of parameters can be used before, but there are some restrictions. Now these restrictions are removed.

Access the jnlp api through applet.

Persistenceservice and downloadservice are supported.

It can control heap memory size, command line parameters, JRE version selection and automatic download. You have the same functions as Java Web Start Software.

Now you can use a statement like the following on the web page:

<APPLET width = "500" Height = "500">
<Param name = "jnlp_href" value = "my_applet.jnlp">
</APPLET>

Calling the applet lifecycle Methods init, start, stop, and destroy will be more specific, and cross-browser behavior has been improved. Fully supports the caching of the applet loader. The legacy applet lifecycle and backward compatibility requirements have been improved.

The applet runs like an application started by Java Web Start. The jnlp_href parameter bridges the JNLP Description between the web page and the applet. In specific aspects such as width and height, Applet labels overlap with JNLP files.

Generally, you should use Deployment Toolkit, which is also a new tool that appears in Java SE 6 Update 10. It can automatically generate HTML for the applet tag. The deployment suggestion guide shows how to use the Deployment Toolkit to easily publish an applet.

  Configure Applet

Now, you can configure the applet more easily in multiple aspects, including the heap memory size, the Java version to be used, the Class Loader cache, the boundary, and others.

<APPLET> This mechanism overlaps with JNLP files when targeting certain parameters. These conflicts can be solved as follows:

Width and height: these attributes are always obtained from <APPLET> instead of JNLP files. This is assumed that the browser knows how big the applet should be displayed on the web page, and only the browser can support the width and height (for example, width = "50%") relative to the page ").

Codebase: If the JNLP file specifies an absolute codebase in the <JNLP> tag, use it. Otherwise, the rules described in the codebase handling section are used for organization.

Code: If the jnlp_href parameter is specified, the main Class Name of the applet will be changed from the main-class parameter to the applet-Desc tag in the JNLP file, and the code attribute will be ignored. Note: This feature allows you to write an applet tag with feedback for the classic Java Plug-in, but in the new Java Plug-in, this tag can use more advanced features. See the "compatibility" section below.

Any

The applet parameter specified by the tag is merged with the parameter specified in the JNLP file. If the <APPLET> tag and JNLP file both specify the same parameter, the version in the <APPLET> tag overwrites the version in the JNLP file, except the java_arguments and java_version parameters.

The new java_arguments and java_version parameters are unnecessary in the JNLP applet. It is replaced by a mechanism that requests the JRE version through the JNLP file or transmits parameters to the JVM. Therefore, the JRE version of the command line parameter and JNLP file request will overwrite the values specified for the applet in HTML.

Specific parameters, such as image and boxbgcolor, are useful during applet startup. It may be better to specify these parameters in HTML instead of JNLP files so that they can be obtained immediately when the web page is loaded without waiting for the JNLP file to be downloaded separately.

In the past, there were limits on setting the maximum heap memory through the Java Control Panel. These restrictions are removed in the new Java Plug-in. Now, the applet can use a lot of heap space as the command line application does.

Specify a heap space larger than the default value:

<APPLET archive = "my_applet.jar" code = "myapplet" width = "300" Height = "300">
<Param name = "java_arguments" value = "-xmx128m">
</APPLET>

Specifies a non-default heap memory and a Java 2D hardware accelerator option. This option is usually used by jogl to apply OpenGL to applet.

<APPLET archive = "my_applet.jar" code = "myapplet" width = "300" Height = "300">
<Param name = "java_arguments" value = "-xmx256m-dsun. java2d. noddraw = true">
</APPLET>

If you like, an applet can be forced into a JVM instance of its own, and isolated from all other applets:

<Param name = "separate_jvm" value = "true"/>

  

This is useful when Porting some desktop applications to a web browser.

You can also make a specific applet run on a specific version of JRE, as shown below:

<J2se version = "1.4 +">
<J2se version = "1.5 *">

This method is useful when you want to replace the selection mechanism of earlier versions (like CLSID in IE) with a JRE or applet of a specific version. If a very old JRE version is requested, the restriction is enforced. If the applet tries to load unsigned code, it will prompt the user.

Note: Because Java Plug-ins supporting JNLP appear for the first time in Java SE 6 Update 10, it is basically meaningless to specify a version like "1.4 +. This makes sense when "1.7 +" is required.

In addition, you can use the <Update> label in the JNLP file to significantly reduce the second and next Startup times.

<Update Check = "background">

In this case, the existing Applet in the cache will be used and the updated version of the application will be downloaded in the background. The new version will be used at the next startup.

The new plug-in can also better customize the image, which will be displayed before the applet is loaded. The image parameter is intended to support animated GIF files. This is described in the special attributes section of the Java Plug-in developers' guide. In addition, the following new parameters are also supported:

Boxborder

A boolean parameter is used to specify whether to draw a border with a width of one pixel on the edge of the applet area before the applet is loaded. The default value is true. We recommend that you set this value to false, especially when an animated GIF is used as a loading image to avoid blinking.

Centerimage

A boolean parameter is used to specify whether to center the loading period image in the applet area, rather than starting from the upper left corner. The default value is false.

Example of using boxborder and centerimage:

<APPLET archive = "large_archive.jar"
Code = "myapplet"
Width = "300" Height = "300">
<! -- Use an animated GIF as an indeterminate progress bar
While the applet is loading -->
<Param name = "image" value = "animated_gif.gif">
<! -- Turn off the box border for better blending with
Surrounding web page -->
<Param name = "boxborder" value = "false">
<! -- Center the image in the applet's area -->
<Param name = "centerimage" value = "true">
</APPLET>

 Compatibility

It is now easier to maintain backward compatibility. You can create programs running on earlier Java Plug-ins, but you only need to provide a complete <APPLET> tag in the same format as the jnlp_href parameter to use these new features. In earlier versions of JRE, The jnlp_href parameter is ignored and the <APPLET> label is used instead. The new Java Plug-in technology will ignore the archive and code parameters, and only use the JNLP file to start the applet.

World Wind applet example

The NASA World Wind Java Applet example created by the World Wind Java Development Group demonstrates how to release a leading class library such as NASA World Wind Java. Likewise, examples are used to illustrate how to use JavaScript to efficiently integrate HTML and Applet content on Web pages.

 

Figure 2. NASA World Wind Applet

This web page contains information about the cascote Mountains (thanks to Wikipedia) and embeds World Wind Java as an applet, shows the locations of mountains in the mountains.

<APPLET id = "wwjapplet" width = 600 Height = 380
Code = "Gov. NASA. WorldWind. Examples. Applet. wwjapplet"
Archive = "backwardcompatibility. Jar">
<Param name = "jnlp_href" value = "wwjapplet. JNLP">
</APPLET>

Wwjapplet is released with the standard World Wind Java release package. As described below, you can choose to write your own Applet Class and embed World Wind in it:

The following is the relevant part of the wwjapplet. JNLP file:

<JNLP href = "wwjapplet. JNLP">
<Resources OS = "Windows">
<Property name = "Sun. java2d. noddraw" value = "true"/>
</Resources>
<Resources>
<J2se href = "http://java.sun.com/products/autodl/j2se" version = "1.4 +"/>
<Jar href = "WorldWind. Jar" Main = "true"/>
<Extension name = "jogl"
Href = "http://download.java.net/media/jogl/builds/archive/jsr-231-webstart-current/jogl.jnlp"/>
</Resources>
<APPLET-descname = "wwj applet"
Main-class = "Gov. NASA. WorldWind. Examples. Applet. wwjapplet"
<! -- Overwritten by the surrounding web page -->
Width = "100"
Height = "100">
</Applet-Desc>
</JNLP>

 Notes:

In this example, WorldWind. jar is used as the main class. Ideally, it is referenced from the NASA website as a JNLP extension, which makes many different applications embedded in world wind or applets share the same JAR file. For details, see the following content.

For its hardware-accelerated 3D graphics, World Wind Java uses Java binding for OpenGL APIs, jogl. Note that the jogl JNLP extension combines only one line of code with the application. Note that on Windows, the driver layer conflicts between OpenGL APIs and DirectDraw/direct3d APIs (this API is used for the default Java 2D implementation on Windows, you must specify the system parameter-dsun. java2d. noddraw = true. This system parameter is required for all applications and Applet programs that use jogl on Windows.

The HTML link on the Web page calls the JavaScript function, which interacts with the applet and directs it to the appropriate Mountain. Below is one of these links:

<A href = "javascript: gotolocation (mount_rainier);"> Mount Rainier </a>
(Southeast of Tacoma, Washington)

After clicking this link, the JavaScript function gotolocation will be called. This function is defined on the same web page:

Function gotolocation (locationstring ){
VaR Params = locationstring. Split (';');
If (Params. Length = 3) // LAT/lon
Getwwjapplet (). gotolatlon (parsefloat (Params [1]),
Parsefloat (Params [2]);

}

The mountain position in the HTML of the web page is decoded as a javascript string. The latitude, longitude, and other visual information are parsed from these strings and passed to the applet. The gotolatlon method is defined in the wwjapplet class. The above method call starts a javascript-to-Java call and passes the parameter from the JavaScript engine to Java. The World Wind applet receives the notification and switches the viewpoint to an appropriate place in an animated manner. Note that the gotolatlon method will return quickly so that the browser does not have to wait for its completion; the animation will run in a separate Java thread.

 

Figure 3. World Wind applet with Mount St. Helen's clicked

As mentioned above, the best way to integrate World Wind Java into your application or applet is to use it as a JNLP extension. This allows many web applications or applets integrated with World Wind Java to share World Wind code resources. To reference the World Wind JNLP extension, you need to add the following statement lines to the <resources> section of the JNLP file of your application or applet:

<Extension name = "WorldWind" href = "http://worldwind.arc.nasa.gov/java/0.4.1/webstart/worldwind.jnlp"/>
<Extension name = "jogl"
Href = "http://download.java.net/media/jogl/builds/archive/jsr-231-webstart-current/jogl.jnlp"/>

Note that the World Wind extension JNLP is of different versions. Therefore, you need to refer to the World Wind documentation or visit the forum to find the latest version of the extension that your JNLP will reference. World Wind Central is a useful resource for the latest World Wind Information.

Class mywwjapplet extends wwjapplet {}

Using World Wind as an extension means you cannot use wwjapplet directly as your main-class. Because of the format semantics of the JNLP file, the main jar (main = "true") must be defined in the main JNLP file. However, it is easy to adapt to this restriction. You can simply create your own wwjapplet subclass (called mywwjapplet) without doing anything:

Place WorldWind. jar in classpath, compile the above class, and then put the class into its own JAR file. Introduce this jar as your main jar, and mywwjapplet becomes your main-class, and then introduce it into world wind as a JNLP extension.

Conclusion

This article introduces the support of Java Plug-ins for JNLP, which provides many new possibilities for the release of Applet. This makes a huge step towards unifying the methods for publishing Java content inside and outside the browser. It has been a long time since they started, and now with support for JNLP, they will be faster and easier to customize than ever before.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.