12 APPLET LIFE CYCLE

Pravin Jain

epgp books

 

Whatis an Applet?

 

Applet is a Panel (inherits from Panel), which can be embedded in a webpage (HTML). What are the additional features of Applet compared to a Panel. It has the capability of interacting with the Browser, and has a life cycle defined by the Browser, when it is viewed in a web page on the browser. The life cycle defined by the help of methods invoked by the browser.

 

For applet to working a browser, the browser must have JVM, the JVM needs to be started before the Applet can be used. When is the JVM started? It is normally started with the help of method main(). In the case of Browser the main 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.

 

Browser as a Container for the Applet

 

Let’s look at the sequence of events within the browser. Firstly, the JVM is already started within the browser with the help of some main method. 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”. ie. 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 create 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, eg. <applet code=”…..”…> some class name is specified. The class name specified should be the name of a class which extends 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 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 default constructor. The public default constructor must be available for the applet class. After creating the instance, the browser would invoke the first method on the instance.

LIFE-CYCLE OF AN APPLET

 

 

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 “public final void setStub(AppletStub stub)” This is the first method invoked on the instance of 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 the Applet instance. Also the AppletStub has a method

 

“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 mentioned between the <applet…> and </applet> tags with the help of methods of 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.

 

The life-cycle of an Applet

 

It is after “setStub” 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 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 web-page containing it is currently being shown. 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 on an active page. 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 “destory()” on the instance of applet to indicate to the applet that it is now going to be destoryed. 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 no. of times. and ultimately the last method invoked is “destroy()”.

  • public final void setStub(AppletStub stub)
  • public fina lvoid init()
  • public final void start()
  • public final void stop()
  • public final void destroy()

Applet Stub

 

In the “java.applet” package there is one class and three interfaces. You 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”:-

 

void appletResize(int width, int height) // invoked to set the size of the corresponding
String  

getParameter(String paramname)

// applet instance.

// returns the value of the given parameter

URL getCodebase() // returns the URL from where the classes can be loaded
URL getDocumentBase() // returns the URL from where  the web page was loaded.
boolean isActive() // gives the active state of the corresponding applet instance

 

The “appletResize()” method may be invoked on the “AppletStub” instance when- ever 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.

 

Applet Tag

 

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 a 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 jdk. in such a case these user-defined classes also needs to be loaded. loading of user-defined classes would be done from the codebase mentioned in the tag. The codebase tag should refer to the directory on the server where all the classes are kept, including the applet class. The archive tag can be used to mention the java archive file on the server which contains all the classes. ie. If you have a jar file which contains all the class files, then it may be mentioned 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.

In Summary:

  • 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.
  • The browser always uses instances of some implementation of “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.
  • The methods which are invoked by the browser on the instance of 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.

 

The “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.

you can view video on APPLET LIFE CYCLE
Suggested Reading:
  1. Core Java Volume 2 by Cay Horstmann & Gary Cornell, Ninth Edition, Pearson Education.
  2. The class of JAVA by Pravin Jain, Pearson Education.
  3. An Introduction to Programming with Java Applets by Elizabeth S. Boese
  4. https://docs.oracle.com/javase/tutorial/deployment/applet/index.html