Come visualizzare le proprie applet in un browser web

Esistono tre modi per integrare applet nelle proprie pagine web. Il primo è usare i tag <applet>, come nell'esempio mostrato qui di seguito:
<applet code="MessageApplet.class" width="150" height="25">
	<param name="message" value="Hello World!">
</applet>
Nella pratica, però, questa modalità non va mai utilizzata per due buoni motivi: primo, per eseguire la applet viene invocata la Virtual Machine propria del browser che visualizza la pagina; secondo, il tag <applet> è deprecato dal World Wide Web Consortium fin dalle specifiche di HTML 4.0.

Lo stesso W3C promuove l'uso del tag <object> come contenitore universale per qualsiasi entità contenente dati che devono essere renderizzati da una applicazione esterna, comprese quindi le applet Java. La sintassi è in verità molto simile a quella vista per il tag <applet>:
<object codetype="application/java" classid="java:MessageApplet.class" width="150"
height="25">
	<param name="message" value="Hello World">
</object>
Questo modo di procedere risolve il problema dei tag deprecati, ma per eseguire la applet invoca ancora la Virtual Machine integrata nel browser.

Il terzo modo è quello che si consiglia: usa il tag <object> e non si affida più alla Virtual Machine del browser (che spesso non è aggiornata, non supporta alcuni componenti, o nel peggiore dei casi fornisce una propria implementazione di alcune specifiche del linguaggio), bensì invoca un piccolo componente fornito da Sun, chiamato Java Plug-in ed incluso nel Java Development Kit. Esso è una piccola JVM che viene invocata dai browser e a cui viene delegata l'esecuzione della applet. Il codice da inserire nelle proprie pagine web è purtroppo un po' più verboso e complesso dei precedenti:
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width="150" height="25">
	<param name="code" value="MessageApplet.class">
	<param name="type" value="application/x-java-applet;version=1.3">
	<param name="message" value="Hello World!">
<!-- Il tag COMMENT è una estensione di IE e fa sì che venga ignorato il contenuto.
Invece Netscape lo vede regolarmente -->
	<comment>
		<embed type="application/x-java-applet" width="150" height="25"
		code="MessageApplet.class">
	</embed>
	</comment>
</object>

Mentre Microsoft Internet Explorer vede e riconosce i tag <object>, Netscape riconosce solo i tag <embed>, ed ecco spiegata la duplicazione del codice. Naturalmente i due tag hanno diverse sintassi, con le quali bisognerà acquisire familiarità.

Per facilitare conversioni di applet già scritte, Sun mette a disposizione un convertitore liberamente scaricabile dalla rete: esso si impegna a trovare i tag <applet> nei file HTML e a convertirli in tag <object> come quello visto qualche riga più sopra. Per saperne di più sul Java Plug-in e per scaricare il convertitore, fate riferimento a http://java.sun.com/products/plugin.


Ad ogni modo, per una spiegazione più approfondita, ecco ciò che c'è scritto a pagina 132 del libro Java Foundation Classes in a Nutshell, scritto da David Flanagan nel 1999, edito da O'Reilly:

When a Java-enabled web browser encounters an <applet> tag, it starts up the embedded Java VM, downloads the class files that implement the applet, and starts running them. This approach has run into difficulties because web browser releases are not synchronized with releases of Java 1.1 before commonly used browsers supported this version of the language, and there are still quite a few browsers in use that support only Java 1.0. It is not at all clear when, or even if, browsers will include support for the Java 2 platform. Furthermore, because of the lawsuit between Sun and Microsoft, the future of integrated Java support in the popular Internet Explorer web browser is questionable.

For these reasons, Sun has producted a product called the Java Plug-in. This product is a Java VM that acts as a Netscape Navigator plug-in and as an Internet Explorer ActiveX control. It adds Java 1.2 support to these browsers for the Windows and Solaris platforms. In many ways, Java support makes the most sense as a plug-in; using the Java plug-in may be the preferred method for distributing Java applets in the future.

There is a catch, however. To run an applet under the Java Plug-in, you cannot use the <applet> tag. <applet> invokes the built-in Java VM, not the Java Plug-in. Instead, you must invoke the Java Plug-in just as you would invoke any other Navigator Plug-in or Internet Explorer ActiveX control. Unfortunately, Netscape and Microsoft have defined different HTML tags for these purposes. Netscape uses the <embed> tag, and Microsoft uses the <object> tag. The details of using these tags and combining them in a portable way are messy and confusing. To help applet developers, Sun distributes a special HTML converter program that you can run over your HTML files. It scans for <applet> tags and convert them to equivalent <embed> and <object> tags.

Consider the simple HTML file we used for the first applet example in this chapter:

<applet code="MessageApplet.class" width="350" height="125">
	<param name="message" value="Hello World">
</applet>
When run through the HTML converter, this tag becomes something like this:
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase="http://java.sun.com/products/plugin/1.2/jinstall-12-win32.cab#Version=1,2,0,0"
width="350" height="125">
	<param name="code" value="MessageApplet.class">
	<param name="type" value="application/x-java-applet;version=1.2">
	<param name="message" value="Hello World">
	
	<comment>
		<embed type="application/x-java-applet;version=1.2"
		pluginspace="http://java.sun.com/products/plugin/1.2/plugin-install.html"
		java_CODE="MessageApplet.class" width="350" height="125"
		message="Hello World">
		</embed>
	</comment>
</object>
When Navigator reads this HTML file, it ignores the <object> and <comment> tags that it does not support and reads only the <embed> tag. When Internet Explorer reads the file, however, it handles the <object> tag and ignores the <embed> tag that is hidden within the <comment> tag. Note that both the <object> and the <embed> tag specify alle the attributes and parameters specified in the original file. In addition, however, they identify the plug-in or ActiveX control to be used and tell the browser from where it can download the Java Plug-in, if it has not already downloaded it.

You can learn more about the Java Plug-in and download the HTML converter utility from http://java.sun.com/products/plugin.

Bibliografia

David Flanagan, "Java Foundation Classes in a Nutshell", O'Reilly, 1999
W3C Recommendation, "HTML 4.01 Specification", The World Wide Web Consortium, 1999