this is the difference between static and non-static

Monday, October 27, 2008

init() is not the First life-cycle method of an Applet

What is an Applet?

Applet is a Panel (inherits from Panel), which can be embedded in a web(HTML) page. 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 is defined by the help of methods invoked by the browser. For applet 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? 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 web page has several html tags.

Lets look at the sequence of events within 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 atleast 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. 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



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 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 in 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 info 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 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 the last method invoked is destory(). In the java.applet package there is one class and three interfaces. you have the applet class, and the 3 interfaces are AppletStub, AppletContext and AudioClip. The AppletStub has methods for getting the information from the applet tag. eg.



void appletResize(int width, int height) // invoked to set the size of the corresponding
// applet instance.
String getParameter(String paramname) // 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 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. The AppletContext has methods to get access to the applet instances in the corresponding web page as:



Enumeration getApplets() // returns an Enumeration containing all the applet
// instances on the corresponding web page
Applet getApplet(String name) // return the applet instance with the name as its id.


What is a URL class? The URL class(defined in the java.net package) encapsulates the information within a url. what are the different parts of a typical url. lets take a typical url.



http://forum.java.sun.com/forum.jspa?forumID=24



So a url has protocol, host, port, file, querystring or reference a reference is mentioned as # followed by a reference within the file.

An instance of URL could be used to parse all the information in a urlstring u can create an instance of URL by calling the constructor with string as parameter



URL(Sring urlstring) // urlstring can be any kind of url.
URL(URL location, String s) // url created relative to an existing URL.


There are other contructors where u can create instance of URL by specifying all the parts of the URL.

Now coming to the AppletContext instance, if the applet wants to interact with the browser, it can invoke methods on the AppletContext instance available to it thru the AppletStub, which was given to it in the setStub method. The methods in the AppletContext are:



void showStatus(String msg) // show a message on the status bar of the browser.
void showDocument(URL webpage, String target) // load the webpage and show on
// the target. as mentioned in the API.


The main activity of a browser is to load resources from a give URL. These resources could be webpage 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 give URL location on the web. It has methods like:



Image getImage(URL imagefile) //downloads the content of image file from the
// URL and creates an instance of Image from the
AudioClip getAudioClip(URL audiofile)//downloads the content of audio file from the
// URL and creates an instance of Audioclip from the
// content.


Now 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



String getParameter(String name)
URL getCodeBase()
URL getDocumentBase()
void resize(int w, int h) // this would in turn invoke appletResized on the
//corresponding stub
void resize(Dimension d)
AppletContext getAppletContext()
boolean isActive()



Methods from the AppletContext



void showStatus(String msg)
Image getImage(URL iamgefile)
Image getImage(URL location, String imagefile) // imagefile relative to the location
AudioClip getAudioClip(URL audioFile)
AudioClip getAudioClip(URL location, String audioFile)
void play(URL u) // gets the AudioClip from the url and calls play on the AudioClip
void play(URL u, String audiofile)


Additionally the Applet has methods which are informative, ie. they may be overridden to provide into about the the applet and its parameters.



String getAppletInfo() // u may return author name, purpose etc.
String[][] getParametersInfo() // u may return a 2-dimension string array describing
// info about the parameters which this applet may look from
// the web page in the param tag


These is another method which may give information about the Locale of the client, ie. the machine on which the browser is running. The instance of Locale typically encapsulates information about the Language and country settings. so u also have a method in the Applet class called.



Locale getLocale().


The AudioClip interface whose instance would be created when an audiofile is downloaded by the browser has following methods:



void play()
void loop()
void stop()



So Applet is a GUI container with capability to interact with the web browser in which it has been enbedded. Now if we look at the way an applet is loaded, we find that the applet can be loaded whenever a user loads a webpage in his browser. The user when loading the webpage may not have the idea whether the page contains an applet or not. applet is a java code which runs inside the users browser, without the knowledge of the user. ie. the user may not specifically invoke the applet code. This could lead to some security issues. The security from applets 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:
1) read, write, delete or create files on the local file system.
2) It cannot open a socket connection to any host other than the host from where it was loaded.
3) it cannot load libraries, cannot terminate the jvm (System.exit()).

As a Container the Applet extends from Panel and so by default it has the FlowLayout as the default LayoutManager.

The JApplet in the javax.swing has one typical feature. ie. it can have Menus. The JApplet has most of the capabilities of JFrame.