What is an applet? An applet is a Panel (inherits from Panel
), which can be embedded on a Web (HTML) page. What are the additional features of Applet
compared to a Panel
? There are no GUI-related additional features in Applet
compared to a Panel
, but it has the capability of interacting with the Browser, and has a life cycle based on which it is managed by the Browser. When an applet is viewed on a Web page on the browser, the browser invokes the life-cycle methods based on the activity taking place within the browser, related to the applet.
For an applet to be able to work in a browser, the browser must have JVM. The JVM needs to be started before the Applet
can be used. When is the JVM started? The JVM is normally started when we start a Java application. A Java application begins with the method main
(). In the case of Applet
there is no main
() method required to be written by the Applet
developer, it is already started by the browser before the Web page is loaded. The browser has its own Java application, which it uses to manage the Java objects like Applet
.
Let us look at the sequence of events within the Java application running inside the browser. Firstly the JVM is already started within the browser with the help of some main
(). When a Web page is loaded which contains at least one tag for an Applet
, then the Java application within the browser would first create an instance of AppletContext
(some class implementing the AppletContext
interface), i.e. one instance of AppletContext
would be created per HTML page, which contains one or more applet tags. The java application would parse the data between the <Applet…..>
and </Applet>
tags and creates an instance of AppletStub
for each such applet tag in the page. This instance has all the information specified between the <Applet…>
and the </Applet>
tags. In the applet tag one of the attributes is code, e.g. <applet code="….."…>
some class name is specified. The class name specified should be the name of a sub-class of the Applet
class. The browser would now send a request to the server from where the Web page was loaded to get the contents of the applet class specified in the code attribute. The class file for the specified applet class is stored on the server and needs to be downloaded to the client machine. After downloading the content of the class file, the browser would now load the downloaded class and then create an instance of the class using its no arg constructor. The public no arg constructor must be available for the applet class. After creating the instance, the browser would invoke the first method on the instance.
Which is the first method invoked on an instance of Applet
? Is it init
()? No. The first method invoked on the instance of Applet
is:
1 public final void setStub(AppletStub stub)
This is the first method invoked on the instance of the applet, and not init(). This method is a final
method in the Applet
class. The instance of AppletStub
created by the browser corresponding to this applet tag is passed to this applet instance. Also the AppletStub
has a method:
1 public AppletContext getAppletContext()
This method would return the AppletContext
instance corresponding to the Web page of this applet. The AppletContext
instance can be used for interaction with the browser. The Applet
instance can get the information within the <applet…>
and </applet>
tags with the help of methods in the AppletStub
instance which is given to it in the setStub
() method. This AppletStub
instance is also used for getting access to the AppletContext
instance for interacting with the browser. So after invocation of setStub
() the Applet
instance has the knowledge about the information in the Applet
tag (mainly the parameters) and the capability to interact with the browser. It is after this that the init
() method is invoked. The init
() method in the applet class has a blank implementation and can be overridden by the sub-class of the applet to do any kind of initialization. The browser maintains the state of the applet instance as being active or inactive. The instance of applet is active when the applet is visible within the browser. The browser then invokes the method start
() and puts the applet in the active state. If the applet is becoming inactive because it is not currently visible, then the browser would invoke the stop
() method on the instance and puts it into inactive state. So start
() and stop
() methods may be invoked by the browser any number of times depending on the activity of the user. Finally when the Web page containing the applet is being destroyed in the browser, then the browser would call the method destroy
() on the instance of the applet to indicate to the applet that it is now going to be destroyed. So the life cycle of an applet starts with setStub
(), followed by init
() followed by start
() and then depending on the user activity may call stop and start any number of times, and ultimately the last method invoked is destroy
().
1 public final void setStub(AppletStub stub) 2 public void init() 3 public void start() 4 public void stop() 5 public void destroy()
AppletStub
In the java.applet package there is one class and three interfaces. We have the Applet
class, and the three interfaces are AppletStub
, AppletContext
and AudioClip
. The AppletStub
has methods for getting the information from the applet tag. The following is the listing of the methods from the AppletStub
:
1 void appletResize(int width, int height) 2 String getParameter(String paramname) 3 URL getCodeBase() 4 URL getDocumentBase() 5 boolean isActive()
The appletResize
() method may be invoked on the AppletStub
instance whenever the associated Applet
instance wants to be resized. The getParameter
() method can be used to get the value of the parameter which is specified in the applet tag. The getCodeBase
() method returns the URL
from where the classes are loaded for this applet. The getDocumentBase
() method returns the URL
from where the document (HTML page) has been loaded. The active state of the applet can be found using the isActive
() method.
The applet tag has attributes like code
, width
, height
, codeBase
, archive
, name
and some other attributes related to the positioning of the applet within the browser. The code
attribute is used for specifying the name of the applet class which should be loaded from the server. width
and height
specify the dimensions of the applet instance within the browser. When an applet class is loaded from the server during the execution of any of the methods in the applet, it may refer to some user-defined classes which are not part of the standard Java API. In such a case these user-defined classes also need to be loaded. Loading of user-defined classes would be done from the location on the server mentioned in the codeBase
tag. The codebase
tag should refer to the directory on the server where all the classes are available, including the applet class. The archive
tag can be used to specify the Java archive file on the server which contains all the classes, i.e. if we have a jar file which contains all the class files, then it may be specified using the archive
attribute in the applet tag. If archive
is mentioned in the tag then the browser would download the entire jar file first on the local machine and then load the classes locally instead of loading the classes over the network. The name
tag is used to assign an identification to the applet instance when it is created. The AppletContext
instance keeps track of all the applet instances which are created in the browser on a single Web page.
AppletContext
An instance of AppletContext
is used for interaction with the browser. The instance of AppletContext
is available to the Applet
from the AppletStub
instance. The methods in the AppletContext
can be used to locate other applets in the Web page, load image and audio resources from any specified URL
, show any document in the browser by specifying its URL, set messages in the status bar of the browser, etc.
The methods to get access to the applet instances in the current Web page are given below:
1 Enumeration getApplets() 2 Applet getApplet(String name)
The method getApplets
() returns an Enumeration
containing all the applets which are loaded in the current Web page. The method getApplet
() can be used to fetch an instance of the Applet
by its name, which has been specified in the Web page.
The showStatus
() method can be used to set a message on the status bar of the browser. The showStatus
() method has a string parameter to specify the message to be set on the status bar as given in the listing below:
1 void showStatus(String msg)
The main activity of a browser is to load resources from a given URL
. These resources could be Web page or some other content type like images and audio. In java.awt. there is a class called Image
, which can be used for rendering on any Component
by using the drawImage
() method of Graphics
class. The AppletContext
has methods to create an instance of Image
from a given URL
location on the web. It has methods like:
1 Image getImage(URL imagefile) 2 AudioClip getAudioClip(URL audiofile) 3 void showDocument(URL webpage, String target)
The getImage
() method returns an instance of Image
by loading the image from the specified URL
. The getAudioClip
() method loads an audio resource from the specified URL
and returns an instance of AudioClip
. The methods of AudioClip
are described in Section 21.6.1. The showDocument
() method can be used to display any resource specified with the URL
instance in the browser. The second parameter can be used to specify the target, where the document may be displayed. The possible values for the target
are _top
, _parent
, _self
and _blank
or the name of any frame in the current Web page.
Applet
CLASSNow if we look at the methods of the Applet
class, it has inherited methods from the Panel
and additional methods are the life-cycle methods plus the methods which would in turn interact with the AppletStub
or AppletContext
. Most of these methods are the same as methods in either AppletStub
or AppletContext
.
Methods from the stub:
1 String getParameter(String name) 2 URL getCodeBase() 3 URL getDocumentBase() 4 void resize(int w, int h) 5 void resize(Dimension d) 6 AppletContext getAppletContext() 7 boolean isActive()
Invoking any of the above methods on an instance of Applet
results in the invocation of the corresponding method on the stub. The resize
() method is not available in the AppletStub
. The invocation of resize
() method on the Applet
results in the invocation of appletResize
() method on the stub.
Methods from the AppletContext
:
1 void showStatus(String msg) 2 Image getImage(URL iamgefile) 3 Image getImage(URL location, String 4 imagefile) // imagefile relative to 5 // the location 6 AudioClip getAudioClip(URL audioFile) 7 AudioClip getAudioClip(URL location, 8 String audioFile) 9 void play(URL u) // gets the AudioClip from the url 10 // and calls play on the AudioClip 11 void play(URL u, String audiofile)
Invoking any of the above methods on an instance of Applet
results in the invocation of the corresponding method on the AppletContext
instance which is available within the stub. The invocation of play
() method on an instance of Applet
results in the invocation of the getAudioClip
() method on the AppletContext
and then the invocation of play
() method on the AudioClip
instance.
Additionally, the Applet
has methods which are informative, i.e. they may be overridden to provide information about the applet and its parameters.
1 String getAppletInfo() 2 String[][] getParametersInfo()
The getAppletInfo
() method is used to return information about the applet, usually overridden to return a copyright message for the applet, and the method getParameterInfo
() is used to return information about the parameters which this applet can understand, when specified in the applet tag. The return type for this method is a two-dimensional array of string, where we have an array for each parameter for each parameter, with three elements. The first element is the parameter name, second is the parameter type and the third is used for information about the parameter.
There is another method which may give information about the Locale
of the client, i.e. the machine on which the browser is running. The instance of Locale
typically encapsulates information about the language and country settings. So we also have a method in the Applet
class called:
AudioClip
The AudioClip
interface whose instance would be created when an audio file is downloaded by the browser has the following methods:
1 void play() 2 void loop() 3 void stop()
The play
() method will play the audio only once, whereas the method loop
() results in a repeatedly playing of the audio. The stop
() method will stop the audio which is currently being played using the AudioClip
instance.
So Applet
is a GUI container with the capability to interact with the Web browser in which it has been embedded.
Now if we look at the way an applet is loaded, we find that the applet can be loaded whenever a user loads a Web page in his browser. The user when loading the Web page may not have the idea whether the page contains an applet or not. Applet is a Java code which runs inside the user’s browser without the knowledge of the user, i.e. the user may not specifically invoke the applet code. This could lead to some security issues. The security within the JVM of the browser is the responsibility of the browser. The Java application which is started initially uses a SecurityManager
to ensure that the applet does not get access to critical resources on the local machine. It would ensure that the applet is not allowed to: read, write, delete or create files on the local file system. It cannot open a socket connection to any host other than the host from where it was loaded. It cannot load libraries, cannot terminate the JVM System.exit(). These security settings can be overridden specifically by a user using the Java’s security policy. The user can decide his own security policy and set it up for all his Java applications including the application launched in the browser. Security policies are not covered in this book.
As a container, the Applet
extends from Panel
and so by default it has the FlowLayout
as the default LayoutManager
.
The GUI components which are used in an Applet
should be the components from the java.awt package. In order to use Swing components in an applet, we can use the JApplet
class, which is a sub-class of the Applet
class. The JApplet
in the javax.swing has one typical feature, i.e. it can have Menus. The JApplet
has most of the capabilities of JFrame
.
Applet
is a special type of Panel
, which can be embedded on a Web page by a browser. It provides a set of life-cycle methods which are invoked by the browser.AppletContext
and AppletStub
along with the instances of Applet
. For each Web page, there would be an instance of AppletContext
created and for each Applet
used in a page there would be a corresponding instance of AppletStub
created in the browser’s JVM.Applet
are setStub
(), init
(), start
(), stop
() and destroy
(). These methods form the life cycle of the Applet
. setStub
() is a final
method and is the first method invoked on the applet. This is used to initialize the applet instance with its corresponding AppletStub
. These AppletStub
is making the AppletContext
and parameter information available to the Applet
. The instance of AppletContext
enables the interaction of Applet
with the browser. The other four life-cycle methods have a blank implementation and can be overridden by the applet developer.init
() method is invoked only once initially after the invocation of the setStub
() method. It is then followed by invocations of start
() and stop
() based on when the applet is becoming active and when it is becoming inactive. The applet is active when it is visible, and inactive when it is not visible on the page. The start
() method is invoked to indicate that the applet’s state has changed to active, and stop
() method is invoked to indicate that the applet’s state has changed to inactive. Finally when the applet is no longer required (when the user moves to another Web page), the applet’s destroy
() method is invoked.Applet
use the instance of AppletStub
and AppletContext
available from the AppletStub
. These methods can be used for fetching parameter values from the Web page associated with the applet. Fetching Image
or AudioClip
instances for a given URL
, etc.JApplet
is a Swing version of the applet and has the capability of using menus.main
() method is the first method invoked on an Applet
whenever it is first loaded.init
() is the first method invoked on an instance of Applet
, when it is used on a Web page of a browser.getParameterInfo
() method returns a two-dimensional giving us the parameters currently used on the Web page for the applet.AppletContext
has a method to fetch all the applets in the current Web page.getStub()
() in the Applet
class to get the instance of AppletStub
associated with the Applet
.AudioClip
can be loaded using the method __________ of the Applet
class.Applet
to make the parameters and context available to the applet.AppletContext
can be used to show any Web page by using an instance of URL
in the browser.Applet
.Applet
.setStub
() method in the life of the Applet
.18.219.81.43