Before the Sun Java plug-in became available, the JVM could vary
considerably from browser to browser and version to version. This
made it very difficult to insure consistent look and behaviour.
This can still be a problem if you want your applets to be used
the widest possible audience. In particular, many MS Windows users
are still saddled with the Java version 1.1 level JVM that has
been the default for the Internet Explorer for many years. Asking
your users to got to java.com
and download the plug-in may not be an attractive idea for people
still using slow dial-up lines.
Challenges for Java Applets include:
- Different browser types having inconsistent look and behaviour
to same Java applet.
- They run on different hosts platforms with different "Look
& Feel" graphical interfaces.
- Many versions of the same browser type exist concurrently.
- Different JVM features such as pre-emptive threading in some
and cooperative threading in others.
To insure that your applet will run and look the way you expect,
regardless of the browser or host, the only solution is to test
your applet on as many browsers and hosts as you can. "Write
once, test everywhere!"
One option is to find out what JVM is running your applet. The
security system in the browser's JVM blocks the applet from finding
many of the system properties values. (See Chapter
14 for an introduction to the SecurityManager
and Chapter
23 for a discussion of System Properties.) The applet code
below gets around this by finding the JVM type via the name of
the security manager:
BrowserTestApplet.java |
//
Derived from TechTip in Java-Pro, Feb.1999
/** A utility to find the browser or plug-in name. **/
public
class BrowserTestApplet extends
java.applet.Applet
{
String
fBrowserName = "";
public void init () {
int browser = getPlatform ();
if (browser == 0)
fBrowserName = "Application
running";
else if (browser == 1)
fBrowserName = "Netscape
browser";
else if ( browser == 2)
fBrowserName = "MS
Internet Explorer";
else if ( browser == 3)
fBrowserName = "Sun
Plugin";
else if (browser == 4)
fBrowserName = "Sun
Appletviewer";
else
fBrowserName = "Unknown
Platform ";
showStatus ("Applet runs on "
+ fBrowserName);
}
/** Use the security manager name to id the
platform.**/
public int getPlatform (){
SecurityManager sm = System.getSecurityManager
();
if (sm == null) return 0;
String str = sm.toString ().toLowerCase
();
if (str.startsWith ("netscape"))return
1;
if (str.startsWith ("com.ms."))
return 2;
if (str.startsWith ("sun.plugin.")) return
3;
if (str.startsWith ("sun.applet.")) return
4;
return -1;
}
public void paint (Graphics g){
g.drawString (browserName, 20, 20);
}
} // class BrowserTestApplet |
Some other browser tips:
- Use yield()
liberally in threads with intense processing to prevent
the applet from dominating the processing on non-preemptive
threaded systems.
- Note that some broswers call Start()/Stop()
even for scrolling and resizing, so you cannot rely on them
to indicate page unloading/loading. (One alternative is
to use Javascripts to call into a Java routine using the
Javascript onload/onunload
signals.)
Latest update: Dec. 6, 2004