More Information


The JWS Portal Page

The portal page (shown in Figure B-9) contains a mix of JavaScript and VBScript, which attempts to detect if JWS is present on the client machine. If it isn't, then the user will be given the option of downloading a JRE. The task is considerably complicated by the many variations between browsers, operating systems, and platforms. Example B-4 is a simplified version of the page.

Example B-4. Simple version of JNLP portal page
     <HTML>     <HEAD><TITLE>Java Web Start Portal</TITLE>     <script language="JavaScript" type="text/javascript">           // check in various ways if JWS is installed;       // decide if the browser is IE on Windows           function insertLink(url, name) {...}       // add a link to the named URL if JWS is present           function installInfo(  ) {...}       // report whether JWS has been found           function nativeReport(  ) {...}       // warn about the use of Windows DLLs by BugRunner and Checkers3D</script>         <script language="VBScript">       // check for various JWS objects in the Windows registry     </script>         </HEAD>     <BODY><H1>Java Web Start Portal</H1>         <script language="JavaScript" type="text/javascript">       installInfo(  );       nativeReport(  );     </script>         <h2>Installation Links</h2>     <P><ul>       <li><script language="JavaScript" type="text/javascript">         insertLink("time.jnlp", "Official US Time");       </script>           <li><script language="JavaScript" type="text/javascript">         insertLink("BugRunner/BugRunner.jnlp", "BugRunner");       </script>           <li><script language="JavaScript" type="text/javascript">         insertLink("Checkers3D/Checkers3D.jnlp", "Checkers3D");       </script>     </ul></P></BODY>     </HTML> 

The JavaScript part of the page sets a flag, javawsInstalled, to the value of 1 if it thinks that JWS is installed. It determines if Windows and Internet Explorer are being used. Three functions are definedinsertLink( ), installInfo( ), and nativeReport( )which are called in the body of the page. If the client's browser is Internet Explorer on Windows, then some VBScript code has a go at setting javawsInstalled.

Setting javawsInstalled in JavaScript

There are four tests attempted:

  1. Check if the JNLP or applet MIME type is set in the browser:

         if (navigator.mimeTypes && navigator.mimeTypes.length) {       if (navigator.mimeTypes['application/x-java-jnlp-file'])         javawsInstalled = 1;       if (navigator.mimeTypes['application/x-java-applet'])         javawsInstalled = 1;     } 

  2. Check if Java is enabled on non-Windows operating systems:

         if (!isWin && navigator.javaEnabled(  ))       javawsInstalled = 1; 

  3. Check for the presence of LiveConnect, an indicator of JWS's presence:

         if (window.java != null)       javawsinstalled = 1; 

  4. Check for the Java plug-in:

         var numPlugs=navigator.plugins.length;     if (numPlugs) {       for (var i=0; i < numPlugs; i++) {         var plugNm = navigator.plugins[i].name.toLowerCase(  );         if (plugNm.indexOf('java plug-in') != -1) {           javawsinstalled = 1;           break;         }       }     } 

None of these approaches is guaranteed to work on every platform, OS, or browser. For example, the JNLP MIME type must be set by an Opera user manually; otherwise, it's not detected even on a machine with JWS. Only Mozilla-based browsers and Opera support LiveConnect. A javaEnabled( ) call will always return TRue on a Windows machine because it detects the less-than-useful Microsoft JVM. Several browsers don't store plug-in information, and even if the Java plug-in is found, it may not be the JVM utilized by the browser.

The JavaScript Functions

installInfo( ) prints a link to one of Sun's download pages for JREs if javawsInstalled is 0. The section in the JWS developers guide called "Creating the Web Page that Launches the Application" (<JAVA_HOME>/docs/guide/jws/developersguide/launch.html) gives an example of the auto-installation of a JRE for Windows from a portal page. I haven't used this approach since it seems better to leave the download choice up to the user.

nativeReport( ) prints a warning if the client OS isn't Windows since BugRunner and Checkers3D rely on DLLs. insertLink( ) adds a JNLP link to the page only if javawsInstalled is 1.

The VBScript Code

The VBScript code is a long multi-way branch that checks the Windows registry for different versions of the JWS. Here's a typical test:

     If (IsObject(CreateObject("JavaWebStart.isInstalled.1.4.2"))) Then       javawsInstalled = 1 

The numerous JWS versions necessitate several branches. The developers guideup to Version 1.4.2refers to JavaWebStart.isInstalled.1, JavaWebStart.isInstalled.2, and JavaWebStart.isInstalled.3, which in all likelihood never existed.

There are tests for the JWS-related MIME types, application/x-java-jnlp-file and application/x-java-applet.

More Information on Portal Pages

The portal page example in the developers guide for Version 5.0 uses a similar approach to the code here; the main difference is the auto-installation of a Windows JRE.

Crucial to portal page code is the ability to detect the browser and OS accurately. A good discussion of these problems can be found at the QuirksMode JavaScript site, http://www.quirksmode.org/index.html?/js/detect.html. A more rigorous way of determining the browser's JRE is to load an applet that interrogates the JVM's java.version property. This approach is described in the Java Plug-in forum thread "How to Detect a Java Plugin from JavaScript" (http://forum.java.sun.com/thread.jsp?thread=168544&forum=30&message=527124). Two online example of this approach, containing many other good Java detection techniques as well, are at http://members.toast.net/4pf/javasniff.html and http://cvs.sdsc.edu/cgi-bin/cvsweb.cgi/mbt/mbt/apps/Explorer/Detect.js?rev=1.2.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net