Last updated 2004-06-30 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : A words : Applet.
Applets must run inside a web browser (or AppletViewer). You cannot run them from the java.exe command line. To spawn a web browser from within an application to get it to display an Applet, see the tips under HTML rendering. Applets are typically automatically downloaded over the web freshly every time they are executed. They can also be run from local hard disk. In contrast, applications cannot be run inside a browser, though it is possible to make a dual mode program that can run either as an Applet or application. Applets run on the client's machine. In contrast, servlets run on the host webserver.
If your Applet needs to do any of the above things, it must be signed.
Now that IE has dropped Java, it is reasonable to ask your clients to upgrade to the latest Java, understanding that some will not. For them, you must develop or at least test on earlier versions. Don't just assume that just because you did not use any new features that you are home free.
Standalone Java applications are not so limited. Exactly what the limitations are is controlled by the security manager in the browser. If the user installs an alternate security class, the Applet may have more powers. The security restrictions are controlled entirely by the browser. There is nothing to stop you from writing a non-conforming browser that has quite different security restrictions for Applets. However, in practice, you write code to sign your jars and bypass each of the five different security schemes used in browsers.
Programmers bitterly complain about these restrictions. The restrictions protect the end user from malicious web Applets they might encounter on the web. Without such protection, vicious Applets could destroy the user's hard disk, print reams of paper, phone out on a spare serial port and rack up long distance bills, go sniffing on the LAN for the company books... You don't want to give those powers to psychotic strangers -- those same people who stay up late at night writing viruses.
<p> <applet code= "MyClass.class" width= "230" height= "240" archive= "Everything.jar,Sub/MoreStuff.zip" codebase= "http://mydomain.com/" vspace= "10" hspace= "10" align= "left" alt= "You need Java to run this Applet" name= "receiver" mayscript > <param name= "favouriteColour" value= "orange" > <img src= "images/NoJava4U.jpg" > You need Java to run this Applet. </applet> <p>
that stray-looking > is not a mistake. It has to be there to close the opening <applet tag This way the Applet has lots of room to display. If you leave them, out your Applet may be rendered off the right edge of the screen, where you probably won't see it.
Here is tip to find the optimal size parameters. Run the Applet as an application. Drag the frame to the optimum size. Use Paint Shop Pro to capture a screen snapshot of the frame, excluding the menu bar. Then look in the lower right corner of the PSP window to discover the size for the <applet height and width tags. Then measure including the menu bar to discover the size for the Frame.setSize( width, height ) when running as an application. It will need about 24 pixels extra height. This trick saves a lot of guesswork and experimentation to home in on the optimal values.
When debugging Applets, remember to click Shift-Reload in your browser, not just plain Reload to attempt re-running with your new version. Shift-Reload supposedly flushes the cache of class files. This usually does not work. You have to exit the browser and restart. It is a good idea to put something unique in every incarnation of your code so you can tell if you are running the old or new code. I do it by flipping a background colour or setting a micro version number.
You can go crazy debugging Applets because the browser sometimes caches old copies of jars, classes, html ... It is best to start each test with at least a fresh loading of your browser, preferably with all its caches deleted using a bat file before each test.
It is best to debug as an application, then at the last minute convert to an Applet.
| HTML Tag | Comments |
|---|---|
| <Applet | |
| code= "MyClass.class" | The name of the class file for the Applet. Make sure the case and name exactly match the name of the *.java file, *.class file, and class name. For a class in a package this would have dots in it, e.g. com.mindprod.mypackage.Myclass.class , but it would not have any directory qualification. You are not allowed to specify an absolute URL or absolute fully qualified hard disk filename. Strange as it sounds, you must specify the trailing .class, though some browsers let you get away without it. All the Sun tutorials do it with the .class. |
| width= "330" | width of entire Applet display in pixels. There is nothing the Applet can do itself to change this. If you needed variable size you would have to resort to JavaScript or a server-side technology to generate HTML pages with the appropriate size. |
| height= "240" | height of entire Applet display in pixels. There is nothing the Applet can do itself to change this. |
| archive= "Everything.jar,Sub/MoreStuff.zip" | Resource file, classes etc. Your ARCHIVE parameter must have a list of the absolute or relative jar files, separated by commas (no spaces). (Watch out! The ARCHIVE tag in <OBJECT is space-separated!) If you have too little or too much qualification, or if you fail to use the file naming conventions of your server, you will be in trouble. You are probably best to use absolute urls or fully qualified hard disk file names. Whether the archive is supposed to be relative to the current HTML directory, the CODEBASE, or all elements of the classpath is unclear. The forms of archive I use most are archive="myapp.jar" archive="../myapp.jar" and archive="somedir/myapp.jar" |
| codebase= "http://mydomain.com/" | I suspect CODEBASE is simply broken in the current implementations. Theoretically it is the absolute or relative URL/directory where class files are, like a one-element classpath. Normally the class files are in the same directory as the html, so you don't need it. In practice, I have found your CODEBASE parameter must have an absolute http:// -style reference to the base directory where the code is stored. The codebase is only needed when your code resides somewhere other than where the html page was loaded from. For a local hard disk, the only thing I could get to work on NT with all browsers and Appletviewers is leaving the CODEBASE out entirely which causes the CODEBASE to default to the same directory as where the enclosing HTML page was loaded from. You may find for your platform you have to code it something like this: file:///C|//MyDir/ or C:\MyDir\. I also further suspect that some browsers will take the a relative CODEBASE as relative to each element of the CLASSPATH, not relative to the current HTML directory. If the user of the signed Applet is behind a firewall, for some strange reason, if he invokes the Applet using the IP rather than the DNS name of the website in the codebase e.g CODEBASE= "http://24.87.56.253/" instead of CODEBASE= "mindprod.com" , all works. Otherwise you get a trustProxy Property error message. |
| vspace= "10" | pixel width of border above and below the Applet |
| hspace= "10" | pixel width of border left and right of the Applet |
| align= "left" | how this Applet aligns, treated like a image |
| alt= "You need Java to run this Applet" | what to display if no Java interpreter available. Normally this would be the same text that appears just before the </applet> tag. This optional attribute specifies any text that should be displayed if the browser understands the APPLET tag but can't run Java applets. |
| name= "receiver" | Name for this Applet so that other Applets can communicate with it. Other Applets would do a Applet Applet.getAppletContext().getApplet( "receiver" ) to get a handle on this Applet. You don't need this parameter if you use Applet Enumeration Applet.getAppletContext(). getApplets() which gets you a list of all the Applets running on the page (including yourself). |
| mayscript | Lets Applet read/write cookies and peek at the page it is embedded in. |
| > | All that stuff above has to be inside the <Applet ... >, but the params may not be. |
| <param name= "favouriteColour" value= "orange" > | The param statements are Java's ode to verbosity. They pass information to
the Applet. There can be as many param statements as you like. Beware of params
with decimal points. <param name= "cost"
value="10.00" > When your Applet
runs in Europe, it will be expecting a comma instead unless you take special
precautions.
Param values cannot contain embedded " or newline characters. If you need them, you must use java.net. URLEncoder to encode the string, then manually include it as the param value, then use java.net. URLDecoder inside the Applet to make sense of the parameter. URLEncoder encodes space as + and special characters as %xx hex, so you can do the encoding in your head once you see a few examples. The catch is, once you hook up URLDecoder to a param, you can't use that param anymore for plain text, at least not completely transparently. Alternatively, you could experiment with encoding the parameters using HTML entities such as " or &#nn |
| <img src= "images/NoJava4U.jpg" > | image to display if no Java interpreter available. |
| You need Java to run this Applet | Text that will display on a really stupid browser that has no idea what an Applet tag is. |
| </Applet> | and finally the ending tag for the |
<applet code="MyApplet.class" width="330" height="240"> </applet>
Make sure you get your < 's and >
's in the right places.
A more typical Applet invocation might look like this:
<applet archive="../mypackage.jar" code="com.mindprod.mypackage.MyClass.class" width="565" height="46" alt="Java needed to display this Applet."> <param name="flavour" value="strawberry"> Java needed to display this Applet. </applet>
To run Applets in Internet Explorer 5.5 or Netscape 4.79 with the Java 1.3 Plugin, you need a hideously complicated syntax using nested <OBJECT> and <EMBED> tags. The best way to generate them is to use the HTML converter program on your simple <APPLET tags. You don't need them to run with the Plug-in 1.4 in Netscape 6.0, Opera 7.21 or IE 6.0.
In theory <APPLET has been deprecated and you should use the more generic, verbose, error-prone <OBJECT tag. <OBJECT has some nice features, like a standby message and a way of providing alternate implementations if the user's browser does not support Java. It can work with serialised Applets. It works for languages and plug-ins other than Java. However, <OBJECT is still not as widely supported as <APPLET , so it is probably wiser to stick with <APPLET for now. See the HTML spec for details.
There is equivalent code for JApplet.
this.getAppletContext().showDocument( url, window );
Your Applet does not get to see the web page. It goes straight to the browser for rendering.
The browser may decide to kill (forget) Applets not on the current page, and then again it may not. Presumably, if you held onto a reference to offscreen Applets in your onscreen Applet, or in a common doubly linked object, those offscreen objects would have to stay alive.
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 7703. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/applet.html | J:\mindprod\jgloss\applet.html | |||