applet returns: the new generation of java plug-ins

13
Applet Returns The new generation of Java Plug-ins Learn about the new Java Plug-in architecture with practical examples in Java and JavaFX SERGE REHEM AND ULISSES TELEMACO TRANSLATED BY MARIA ALICE, REVIEWED BY FABIANE NARDON Abstract: Kind of forgotten among so many news in RIA world (Adobe Flash/Flex/Air, Microsoft Silverlight, JavaFX, ...), the new architecture of Plug-ins can bring back Applets as an option to be considered. They can be used to create a variety of applications, from small components to be embedded in traditional web pages (such as “stylish” buttons, animations or graphs) to more complete applications (as filling in a form or a questionnaire) similar to a Desktop applications. The current architecture offers pretty much the same advantages you can find in Java WebStart applications. We will start recollecting the old way of building up Applets and showing an overview of this technology. Then, we will present what is new on the new architecture of Java Plug-in, showing practical examples in Java and JavaFX tested in Windows, Linux and Mac OS. We will see how the usage of JNLP – the same format used in Java WebStart applications – allows the customization of properties as, for example, the selection of an specific JVM to be used, the usage of an own address space for each Applet, the possibility of providing parameters to the JVM, the option to change the default background image from Java showed in loading moment, and more. One of the great new features is a “drag-drop” option that allows an Applet to be dragged to “outside the browser” making it to be executed as an independent application. At the end, we will suggest additional readings for those who aspire more deep learning in this subject. When Java was released, fifteen years ago, Java “mini-applications” executed in browsers were Java “big hit”. At that time the common discussion was “what is best? Java Applets or Microsoft's Activex?”. A long time have passed since then and there was little evolution in Applets technology (well, we better not even talk about ActiveX...). The Java 1.6 update 10 came to change radically this scenario, presenting the new generation of Java plug-ins. This article will reveal the most important changes in the plug-ins architecture, showing practical examples why we believe Applets are definitively back. Remembering Applets An Applet is a special Java program, embedded in an HTML page, which is executed in the context of a Java enabled browser. In other words, an Applet is a subclass of java.applet.Applet (or javax.swing.JApplet), which provides an interface between the applet and the browser environment. Although going deep in Applet technology concepts isn't the goal of this article, it is useful to remind the principal concepts. So, we will build a traditional Applet, then we will introduce gradually the new Java Plug-ins architecture. The following code creates a simple Applet that displays on screen the message “Hello Java Bahia!”. 1

Upload: serge-rehem

Post on 18-Nov-2014

5.774 views

Category:

Technology


0 download

DESCRIPTION

This article will reveal the most important changes in the Java Plug-in architecture since JDK1.6update10, showing practical examples why we believe Applets are definitively back.

TRANSCRIPT

Page 1: Applet Returns: The new generation of Java Plug-ins

Applet ReturnsThe new generation of Java Plug-ins

Learn about the new Java Plug-in architecture with practical examples in Java and JavaFX

SERGE REHEM AND ULISSES TELEMACO

TRANSLATED BY MARIA ALICE, REVIEWED BY FABIANE NARDON

Abstract:

Kind of forgotten among so many news in RIA world (Adobe Flash/Flex/Air, Microsoft Silverlight, JavaFX, ...), the new architecture of Plug-ins can bring back Applets as an option to be considered. They can be used to create a variety of applications, from small components to be embedded in traditional web pages (such as “stylish” buttons, animations or graphs) to more complete applications (as filling in a form or a questionnaire) similar to a Desktop applications. The current architecture offers pretty much the same advantages you can find in Java WebStart applications.We will start recollecting the old way of building up Applets and showing an overview of this

technology. Then, we will present what is new on the new architecture of Java Plug-in, showing practical examples in Java and JavaFX tested in Windows, Linux and Mac OS. We will see how the usage of JNLP – the same format used in Java WebStart applications – allows the customization of properties as, for example, the selection of an specific JVM to be used, the usage of an own address space for each Applet, the possibility of providing parameters to the JVM, the option to change the default background image from Java showed in loading moment, and more. One of the great new features is a “drag-drop” option that allows an Applet to be dragged to “outside the browser” making it to be executed as an independent application. At the end, we will suggest additional readings for those who aspire more deep learning in this subject.When Java was released, fifteen years ago, Java “mini-applications” executed in browsers were

Java “big hit”. At that time the common discussion was “what is best? Java Applets or Microsoft's Activex?”. A long time have passed since then and there was little evolution in Applets technology (well, we better not even talk about ActiveX...). The Java 1.6 update 10 came to change radically this scenario, presenting the new generation of Java plug-ins. This article will reveal the most important changes in the plug-ins architecture, showing practical examples why we believe Applets are definitively back.

Remembering Applets

An Applet is a special Java program, embedded in an HTML page, which is executed in the context of a Java enabled browser. In other words, an Applet is a subclass of java.applet.Applet (or javax.swing.JApplet), which provides an interface between the applet and the browser environment. Although going deep in Applet technology concepts isn't the goal of this article, it is useful to remind the principal concepts. So, we will build a traditional Applet, then we will introduce gradually the new Java Plug-ins architecture.

The following code creates a simple Applet that displays on screen the message “Hello Java Bahia!”.

1

Page 2: Applet Returns: The new generation of Java Plug-ins

import javax.swing.JApplet;import javax.swing.JLabel;

public class HelloJavaBahia extends JApplet { @Override public void start() { getContentPane().add(new JLabel("Hello " + getParameter("name") + "!")); }}

To execute it in a browser it'll be necessary to create an HTML page and use a tag <applet>1. Example:

<html> <body> <h1>My first Applet</h1> <applet code="HelloJavaBahia.class" width="200" height="50"> <param name="name" VALUE="JavaBahia"/> Your browser doesn't support Applets! </applet> </body></html>

And the result would be:

Figure 1. Traditional Applet

Note: This article was originaly writen in brazilian portuguese, that's why the screenshots figures aren't in english

Known Limitations

Traditional Applets have several limitations. Some of them come from the fact that the JRE and the browser are executing in the same address space:

• The browser “freezes” when JRE is started for the first time;

1 The tag <applet> is recommended to environments where browser portability is an important requirement (the Internet, for example). In more controlled environments, as Intranets, it's possible to use the tags <object> too, which it'll work just in Internet Explorer, and <embed>, for the Mozilla browsers family.

2

Page 3: Applet Returns: The new generation of Java Plug-ins

• A blocking in JRE also blocks the browser;• Just one version of JRE can be used each time;• The developer doesn't control which version of JRE is being used, nor can pass parameters

to JVM (to, for example, memory configuration);

Another limitations from security restrictions: • The Applets don’t have access to the File Systems and thus can't read or write files on

client's machine. • The Applets can't establish connections with another machines, except with the Applet

origin server.• The Applet can't start or finalize a program on client's machine.

New Generation of Java Plug-inThe new generation of Java Plug-in brings a totally redesigned architecture and solves many

known problems from the previous generation. Among the most important advantages from this new architecture, we emphasize the fact that the JVM is now executed in a browser independent context. So, if a problem is generated by a specific Applet, the new Java Plug-in architecture can deal with the problem without affecting browser operation. The Table 1 summarizes the most important advantages of this new architecture.

Table 1 – Advantages of the new Java Plug-in architectureMore Reliability The Applet can be configured to use a browser independent JVM. In this case, Java

Plug-in detects and deals the problems without affecting the browser.

Support to JNLP The new Java Plug-in provides the same features provided to Java WebStart applications, allowing to load Applets directly from JNLP (Java Network Launching Protocol) files. It is possible to even access the JNPL APIs for persistence, access to the system and other functionalities.

Command line arguments via Applet

It is possible to specify parameters for the JVM (similar to parameters passed by command line) via Applet. Therefore, it's possible to configure, for example, memory heap size or hardware 2D acceleration functionalities.

Support to multiple versions of JRE

Now it's possible to explicit which version of JRE must be used by the Applet. Each Applet individual instance can require a specific version of JRE.

Better Java/JavaScript communication

There was a considerable improvement between JavaScript and Java Applet communication with guaranteed compatibility across different browsers. The JavaScript API can be used to: detect JRE, install/update JRE, execute Applets, set Java WebStart applications, etc.

Better life cycle control of an Applet

The behavior of life cycle control methods (init, start, stop and destroy) in the Applet API are now more deterministic and portable across different browsers.

Better memory space configuration

The maximum memory that could be configured to an Applet was limited in previous versions of Java Plug-in. This limitation was eliminated in the new version and the Applets can be configured to use the same amount of memory as command line applications can.

Better user experience Applets are loaded in background, improving browser responsiveness. The Applet appears in the web page just when it is ready to be executed.

Improved support to Windows Vista

Signed Applets can have the same privileges of the regular applications in Windows Vista.

Applets with JNLPThe Java Network Launch Protocol, JNLP, is the default form Java WebStart applications

distribution and installation. The new Java Plug-in architecture allows the same JNLP descriptor

3

Page 4: Applet Returns: The new generation of Java Plug-ins

utilization, what will cause the Applet to behave as a conventional application set via WebStart. Going back to the example we showed before, we could rewrite the tag <applet> the following way:

 <applet code="javabahia.javaplugin.HelloJavaBahia" width="200" height="50">   <param name="name" value="JavaBahia">   <param name="jnlp_href" value="hellojavabahia.jnlp"> </applet>

In this second example, we suppose that the class HelloJavaBahia belongs to the package javabahia.javaplugin and it's compiled in newjavaplugin.jar. Note that the parameter jnlp_href points to JNLP file in the code below. Let's analyze it with more detail.

1. <?xml version="1.0" encoding="UTF-8"?>2. <jnlp href="Hellojavabahia.jnlp">3. <information>4. <title>Hello JavaBahia Applet</title>5. <vendor>JavaBahia.org</vendor>6. </information>7. <resources>8. <j2se version="1.4+" href="http://java.sun.com/products/autodl/j2se" />9. <jar href="newjavaplugin.jar" main="true" />10. </resources>11. <applet-desc name="Hello JavaBahia Applet"12. main-class="javabahia.javaplugin.HelloJavaBahia"13. width="1"14. height="1" /> 15. </jnlp>

The element <information> (lines 3 to 6) is required and contains information about the Applet author and vendor. The lines 7 to 10 (tag <resources>) show interesting new features: The tag <j2se version="1.4+">2 indicates the JVM3 minimum version accepted to execute the Applet. The tag <jar> points to the JAR file that contains the Applet runnable code. The tag <applet-desc> (lines 11 to 14) specifies the Applets name (attribute name) and indicates the main class name (attribute main-class) and can overload some parameters of the tag <applet> from HTML file. In this example, although provided, the attributes width and height from JNLP are of no effect, prevailing the properties defined in the tag <applet>. This happens because it is assumed that the browser knows better how to accommodate the Applet in the web page, and only the browser can calculate height and width associated with the page (ex: width=“40%”). See the official page of new Java Plug-in for more details about all supported parameters and how to solve other types of conflicts.

JVM Configuration Parameters

Besides deploying an Applet from a JNPL file, it's possible to use the tag <applet> with a set of parameters (tag <param>) that are JVM specific. We'll show the three main types of JVM configuration: separate_jvm, java_version and java_arguments. See the example below:

2 The tag <j2se> also supports command line parameters to be passed to the JVM, such as system properties or maximum heap size. In case of no active JVM can support the desired task, a new instance will be launched. See the references at the end of this paper for more details.

3 Since the support to JNLP in Java Plug-in will only be available from version 1.6update10, specifications as “1.4+” are basically worthless. Version specification will be more useful when we can specify for example “1.7+” (from version 1.7), “1.7*” (any update from 1.7 family) or “1.6.0_13” (just in 1.6 version update 13).

4

Page 5: Applet Returns: The new generation of Java Plug-ins

<applet code="javabahia.javaplugin.HelloJavaBahia" width="200" height="50"> ... <param name="separate_jvm" value="true" /> <param name="java_version" value="1.5+" /> <param name="java_arguments" value="-Xmx128m" /></applet>

The first parameter (separate_jvm) states that the Applet can be executed in its own instance of the JVM, separated from all others Applets. This can be useful when making Desktop applications run in the browser. The second parameter (java_version) allows to specify the minimum version of the necessary JVM to execute the Applet. And the third parameter (java_arguments) was used to configure the maximum of memory that the Applet can use in execution time.

“Drag-Drop” FeatureOne of most interesting new features of the Java Plug-in new architecture is the possibility of drag

an Applet and drop it in the Desktop. However, this is an experimental functionality and there is no guarantees that it will be supported in future Java Plug-in versions. To enable this feature just add the parameter <param name=“draggable” value=“true” /> inside the tag <applet>. To test it, use the combination (Alt key + mouse left button) and drag the Applet to your Desktop. You will see a little close button on the top right corner of your screen. The Applet API allows to even customize the act of “dragging”, the close button and the loading image (replacing the traditional cup of coffee from Java). While the page with the Applet is opened, it may return to its origin place. If the page is closed,

reloaded or navigated to another, the Applet is disconnected from the browser and starts to execute in an independent way.

In some Linux platforms this combination (Alt key + mouse left button) is normally used to move windows, then it is necessary to change the behavior of this combination in the Operational System windows manager. Optionally, it is possible to customize the key combination in the Applet. To do that, implement in the Applet a public method with the interface public boolean isAppletDragStart(MouseEvent e);

Integration with JavaScript

The LiveConnect specification, used to integrate JavaScript and Java Applet, was totally re-implemented. The new architecture guarantees more reliability, compatibility among browsers and better performance, either for JavaScript calling Applets as for Applets calling JavaScripts methods.See an example of JavaScript invoking an Applet method.

import javax.swing.JApplet;import javax.swing.JLabel;

public class HelloJavaBahia extends JApplet {

@Override public void start() { getContentPane().add(new JLabel("Hello " + getParameter("name") + "!")); } public String upperCase(String s){ return s.toUpperCase(); }}

5

Page 6: Applet Returns: The new generation of Java Plug-ins

The call for the method upperCase is done transparently:

<html> <body> <h1>My first Applet</h1> <applet id="app" code="HelloJavaBahia.class" archive="HelloJavaBahiaApplet.jar" width="200" height="50"> <param name="name" VALUE="Java Bahia"/> Your browser doesn't support Applets! </applet> <script language="javascript"> var msg = app.upperCase("Hello Java Bahia!!!"); document.write(msg); </script> </body></html>

And the result can be seen in Figure 2 bellow:

Figure 2. Integration between JavaScript and Applet

In order to make this article not too long, we won't detail here the integration with JavaScript. For more information (as, for example, how to call a JavaScript method inside an Applet), please see the LiveConnect documentation linked at the end of this paper.

Preparing the browser The new Java Plug-in architecture was designed to work in Internet Explorer 6 and 7, in Windows

XP and Vista, and in Firefox 3, in Windows XP and Vista, Solaris and Linux.The configuration of Internet Explorer must be done through the Java Control Panel, accessible

through Windows XP or Vista Control Panel, enabling new generation of Java Plug-in in “Advanced” tab, as in Figure 3.

6

Page 7: Applet Returns: The new generation of Java Plug-ins

Figure 3. Enabling new Java Plug-in in Windows

In Linux or Solaris, the Firefox 3 configuration is a little more complicated. You have to create manually a symbolic link (symlink) to the new Java Plug-in. For that, open a shell and go to plugins paste in your Firefox installation directory (for example: /etc/lib/firefox-3.0.10/plugins) and execute the command ln -s pointing to your directory <JRE_HOME | JDK_HOME>/jre/lib/{i386,sparc}/libnpjp2.so. For example:

cd /opt/firefox/pluginsln -s /usr/jvm/lib/sun-java6/jre/lib/i386/libnpjp2.so

7

Page 8: Applet Returns: The new generation of Java Plug-ins

JavaFX Applets with NetBeans

Exploring the possibilities of new Java Plug-in further, we'll see how to create an Applet with JavaFX, a multimedia software platform for creating Rich Internet applications, which can be executed in several different devices. Totally based in Java, JavaFX allows to create applications with high visual and interactivity quality to execute in desktop, browser, cellular telephones, video-games, Blu-rays and even in digital TV. Deepening in this theme is out of scope of this paper, so we suggest a visit to http://javafx.com for those who wish to learn more about JavaFX.At the moment we write this article, NetBeans is the only IDE that supports JavaFX. Using

NetBeans 6.8 (the most recent version when we wrote this article), create a new project of JavaFX Category and choose JavaFX Script Application. Fill the information as in Figure 4.

Figure 4. Creation of a JavaFX project in NetBeans 6.8

After creating HelloJavaBahiaJavaFX project, click with the right button in your name and choose the option Properties. In the Run category, create a new Configuration clicking in New and type Applet. Selects the option Run in Browser. Your screen should be as in Figure 5.

Page 9: Applet Returns: The new generation of Java Plug-ins

Figure 5: Configuration of JavaFX project in Netbeans 6.8

Following, in the Properties window, choose the Application category and type the information as shown in Figure 6. Note that the option Draggable Applet should be selected.

Figure 6. Altering project preferences

The code automatically generated by NetBeans can be executed already. It'll open an HTML page in the browser containing JavaFX code that displays the text “Application Content” below the tittle HelloJavaBahiaJavaFX in bold. In the dist directory from your project, note that it was created an HTML file and two JNLP files (one to execute the application as an Applet in the browser and another to execute it as Java WebStart application).

Page 10: Applet Returns: The new generation of Java Plug-ins

Figure 7. Files automatically generated in NetBeans build

See below the Java FX Script code from the Main.fx file. The lines highlighted in bold were modified or added to give a better visual behavior to the Applet, changing front and background colors and adding an effect called reflection.

Main.fx1. package hellojavabahiajavafx;2.3. import javafx.stage.Stage;4. import javafx.scene.Scene;5. import javafx.scene.text.Text;6. import javafx.scene.text.Font;7. import javafx.scene.effect.Reflection;8. import javafx.scene.shape.Rectangle;9. import javafx.scene.paint.Color;10.11. Stage {12. title: "Hello JavaBahia JavaFX"13. scene: Scene {14. width: 25015. height: 8016. content: [17. Rectangle {18. x: 0, y: 019. width: 200, height: 8020. fill: Color.BLACK21. }22. Text {23. font: Font {24. size: 1625. }26. x: 1027. y: 3028. content: "Hello JavaBahia"29. fill: Color.WHITE30. effect: Reflection {fraction: 0.9 topOpacity: 0.9 topOffset: 0.1};31. }32. ]33. }34. }

The HelloJavaBahiaJavaFX_browser.jnlp file that you can see below, has several tags that we didn’t discuss yet. They are pretty much self-explanatory, so we won’t explain them. If you want more information about them, check the references at the end of this article.

Page 11: Applet Returns: The new generation of Java Plug-ins

1.<?xml version="1.0" encoding="UTF-8"?>2.<jnlp spec="1.0+" codebase="file:/home/serge/NetBeansProjects/HelloJavaBahiaJavaFX/dist/" 3. href="HelloJavaBahiaJavaFX_browser.jnlp">4. <information>5. <title>HelloJavaBahiaJavaFX</title>6. <vendor>JavaBahia</vendor>7. <homepage href=""/>8. <description>HelloJavaBahiaJavaFX</description>9. <offline-allowed/>10. <shortcut>11. <desktop/>12. </shortcut>13. </information>14. <security>15. <all-permissions/>16. </security>17. <resources>18. <j2se version="1.5+"/>19. <property name="jnlp.packEnabled" value="true"/>20. <property name="jnlp.versionEnabled" value="true"/>21. <extension name="JavaFX Runtime" href="http://dl.javafx.com/1.2/javafx-rt.jnlp"/>22. <jar href="HelloJavaBahiaJavaFX.jar" main="true"/>23. </resources>24. <applet-desc name="HelloJavaBahiaJavaFX" main-

class="com.sun.javafx.runtime.adapter.Applet" 25. width="200" height="200">26. <param name="MainJavaFXScript" value="hellojavabahiajavafx.Main">27. </applet-desc>28. <update check="background">29. </jnlp>

Let’s modify the HelloJavaBahiaJavaFX.html file so we can explain a few interesting aspects of this architecture. Note that the lines 14 to 26 were commented out because NetBeans doesn't generate code using the tag <applet> that we are exploring in this article. In its place, it is used the tag <script> and inside it, the JavaFX code that load the script Main.fx in the browser. In this snippet of code, we added a call using the tag <applet> and providing the parameter draggable=“true”.

1.<html>2. <head>3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">4. <title>HelloJavaBahiaJavaFX</title>5. </head>6. <body>7. <h1>HelloJavaBahiaJavaFX</h1>8. <applet code="HelloJavaBahiaJavaFX" archive="HelloJavaBahiaJavaFX.jar" width="200" 9. height="60">10. <param name="jnlp_href" value="HelloJavaBahiaJavaFX_browser.jnlp"/>11. <param name="draggable" value="true"/>12. </applet>13. <!--14. <script src="http://dl.javafx.com/1.2/dtfx.js"></script>15. <script>16. javafx(17. {18. archive: "HelloJavaBahiaJavaFX.jar",19. draggable: true,20. width: 200,21. height: 200,22. code: "hellojavabahiajavafx.Main",23. name: "HelloJavaBahiaJavaFX"24. }25. );26. </script>27. -->28. </body>29. </html>

At last, the final result of our “AppletFX” will be as seen in Figure 8.

Page 12: Applet Returns: The new generation of Java Plug-ins

Figure8. Visualization of the Applet built with JavaFX

ConclusionThis paper aimed to bring back to memory how traditional Applets works and present new

possibilities to Java and JavaFX that are available in version Java SE 1.6 update 10 with the new Java Plug-In. After a long time of stagnation, it is interesting to see that Sun is trying to repair the damage and now provides interesting alternatives that may make the developers to reconsider the usage of Applets in his/her applications and Web pages.

Linkshttp://java.sun.com/docs/books/tutorial/deployment/applet/index.htmlApplet Tutorial http://java.sun.com/j2se/1.4.2/docs/guide/misc/applet.htmlSintaxe completa da tag Applet http://java.sun.com/javase/6/docs/technotes/guides/plugin/developer_guide/contents.htmlJava Plugin Guide, before Jdk1.6u10 http://java.sun.com/javase/6/docs/technotes/guides/jweb/index.htmlJava WebStart Applets and Aplications for the Jdk1.6u10 http://java.sun.com/developer/technicalArticles/javase/6u10_appletsThe new feature Applet Draggable no Java SE 6 Update 10 Plug-Inhttps://jdk6.dev.java.net/plugin2Version notes of New Generation Java Plug-In http://developers.sun.com/learning/javaoneonline/2008/pdf/TS-6290.pdfPresentation in JavaOne 2008 about new Java Plug-inhttps://jdk6.dev.java.net/plugin2/jnlp/Suport to JNLP in new Java Plug-in https://jdk6.dev.java.net/plugin2/version-selection/Version selection of the JVM with the new Java Plug-inhttps://jdk6.dev.java.net/plugin2/liveconnect/Integration between Java Applet and Javascripthttps://scenegraph-demos.dev.java.net/demo-applets.htmlApplets Demonstration using new Java Plug-in

Page 13: Applet Returns: The new generation of Java Plug-ins

Serge Rehem ([email protected]) is PMP, MBA in Administration at Unifacs, specialist in Distributed Systems at UFBA. Serpro/Salvador Analyst since 1997, currently coordinate the technical team of Demoiselle Framework, in the projection of Technology Strategic Coordination (Cetec) in Salvador's region. It's JavaBahia leader and maintains the blog bazedral.blogspot.com, about collaborative work at companies.

Ulisses Telemaco ([email protected]) is the creator, founder e maintainer of JEEBrasil project. He has a degree in Computer Science at UFRN. Majoring Mechatronics at UFBA. Speaker in several Java events such as JustJava, Abaporu, Natal Java Day, Salvador Java Day, Maratona 4 Java, among others. Participated actively of the JUGs, JavaBahia e JavaRN organization. Currently works as Software Architecture at OWSE Informática in Rio de Janeiro. Reinforces the SouJava team in the wonderful land.

Maria Alice Leal ([email protected]) is a Computer Engineering student at UEFS. An enthusiastic about technologies, JAVA, including computing. And had already taught an informatics introductory course of including nature at UATI (Third-Age Open University) project at UEFS.