Applet[an error occurred while processing this directive]
Advantages of Applet:
Disadvantages of Java Applet:
Life Cycle of Applet: Applet
runs in the browser and its lifecycle method are called by JVM when it is loaded
and destroyed. Here are the lifecycle methods of an Applet:
stop(): This method can be called multiple times in the life cycle of an Applet.
destroy(): This method is called only once in the life cycle of the applet when applet is destroyed.
() method: The life cycle of an applet is begin on that
time when the applet is first loaded into the browser and called the init()
method. The init() method is called only one time in the life cycle on an
applet. The init() method is basically called to read the PARAM tag in the html
file. The init () method retrieve the passed parameter through the PARAM tag of
html file using get Parameter() method All the initialization such as
initialization of variables and the objects like image, sound file are loaded in
the init () method .After the initialization of the init() method user can
interact with the Applet and mostly applet contains the
() method: The stop() method can be called multiple times in the life
cycle of applet like the start () method. Or should be called at least one time.
There is only miner difference between the start() method and stop () method.
For example the stop() method is called by the web browser on that time When the
user leaves one applet to go another applet and the start() method is called on
that time when the user wants to go back into the first program or Applet.
Creating First Applet Example: First of all we will know about the applet. An applet is a program written in java programming language and embedded within HTML page. It run on the java enabled web browser such as Netscape navigator or Internet Explorer.
In this example you will see, how to write an applet program. Java source of applet is then compiled into java class file and we specify the name of class in the applet tag of html page. The java enabled browser loads class file of applet and run in its sandbox.
Here is the java code of program :
Here is the HTML code of the program:
Passing Parameter in Java Applet: Java applet has the feature of retrieving the parameter values passed from the html page. So, you can pass the parameters from your html page to the applet embedded in your page. The param tag(<parma name="" value=""></param>) is used to pass the parameters to an applet. For the illustration about the concept of applet and passing parameter in applet, a example is given below.
In this example, we will see what has to be done in the applet code to retrieve the value from parameters. Value of a parameter passed to an applet can be retrieved using getParameter() function. E.g. code:
Printing the value:
Then in the function paint (Graphics g), we prints the parameter value to test the value passed from html page. Applet will display "Hello! Java Applet" if no parameter is passed to the applet else it will display the value passed as parameter. In our case applet should display "Welcome in Passing parameter in java applet example." message.
Here is the code for the Java Program :
There is the advantage that if need to change the
output then you will have to change only the value of the param tag in html file
not in java code.
Output after running the program :