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à. 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. <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 |
|
|
|