An applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. Java applets were introduced in the first version of the Java language in 1995. An applet is typically embedded inside a web-page and runs in the context of the browser. An applet must be a subclass of the java.applet.Applet class, which provides the standard interface between the applet and the browser environment. Java applets are executed in a sandbox by most web browsers, preventing them from accessing local data. A Java applet extends the class java.applet.Applet, or in the case of a Swing applet, javax.swing.JApplet. The class must override methods from the applet class to set up a user interface inside itself. Java applets can run in a Web browser using a Java Virtual Machine or in Sun’s AppletViewer, a stand-alone tool for testing applets.
Life Cycle of an Applet: Basically, there are four methods in the Applet class on which any applet is built.
init: This method is intended for whatever initialization is needed for your applet. It is called after the param attributes of the applet tag.
start: This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages.
stop: This method is automatically called whenever the user moves away from the page containing applets. You can use this method to stop an animation.
destroy: This method is only called when the browser shuts down normally.
Java applets can be executed by browsers for many platforms, including Windows, Unix, Mac OS and Linux. It is also trivial to run Java applet as an application with very little extra code. This has the advantage of running a Java applet in offline mode without the need for internet browser software and also directly from the development IDE.
The API is provided by the javax.swing.JApplet class and the java.applet.AppletContext interface.
Applets can use these APIs to do the following:
- Be notified by the browser of milestones.
- Load data files specified relative to the URL of the applet or the page in which it is running.
- Display short status strings.
- Make the browser display a document.
- Find other applets running in the same page.
- Play sounds.
- Get parameters specified by the user in the <APPLET> tag.
Example of a simple APPLET tag:
<applet code=”MyApplet.class” width=100 height=140></applet>
This tells the viewer or browser to load the applet whose compiled code is in MyApplet.class (in the same directory as the current HTML document), and to set the initial size of the applet to 100 pixels wide and 140 pixels high.
Complex example of an APPLET tag:
code=”NervousText.class” width=400 height=75>
<param name=”text” value=”Welcome to HotJava!”>
If you were using a Java-enabled browser such as HotJava,
you would see dancing text instead of this paragraph.
This tells the viewer or browser to load the applet whose compiled code is at the URL http://java.sun.com/applets/NervousText/1.1/NervousText.class, to set the initial size of the applet to 400×75 pixels. The viewer/browser must also set the applet’s “text” attribute (which customizes the text this applet displays) to be “Welcome to HotJava!” If the page is viewed by a browser that can’t execute Java applets, then the browser will ignore the APPLET and PARAM tags, displaying only the HTML between the <param> and </applet> tags (the alternate HTML).
Advantages of Java Applet:
- Java Applet simple to make it work on Linux, Windows and Mac OS i.e. to make it cross platform
- Same applet can work on “all” installed versions of Java at the same time, rather than just the latest plug-in version only. However, if an applet requires a later version of the JRE the client will be forced to wait during the large download.
- java Applet supported by most web browsers
- Java Applet will cache in most web browsers, so will be quick to load when returning to a web page but may get stuck in the cache and have issues when new versions come out.
- Applet can have full access to the machine it is running on if the user agrees
- Applet can improve with use: after a first applet is run, the JVM is already running and starts quickly, benefitting regular users of Java but the JVM will need to restart each time the browser starts fresh.
- Applet can move the work from the server to the client, making a web solution more scalable with the number of users/clients developers can develop and debug an applet direct simply by creating a main routine (either in the applet’s class or in a separate class) and call init() and start() on the applet, thus allowing for development in their favorite J2SE development environment. All one has to do after that is re-test the applet in the appletviewer program or a web browser to ensure it conforms to security restrictions.
- an untrusted applet has no access to the local machine and can only access the server it came from. This makes such applet much safer to run than standalone executable that it could replace.
Disadvantages of Java Applet:
- Java Applet requires the Java plug-in, which is not available by default on all web browsers.
- Prior to version 6u12, Sun did not provide a 64-bit version of its Java plug-in, forcing users to use the 32-bit plugin with a 32-bit browser.
- Java Applet cannot start until the Java Virtual Machine is running, and this may have significant startup time the first time it is used.
- Some organizations only allow software installed by the administrators. As a result, many users cannot view applets by default.