This page discusses - IntroductionThis page discusses - Introduction
Applet is java program that can be embedded into HTML pages. Java applets runs on the java enables web browsers such as mozila and internet explorer. Applet is designed to run remotely on the client browser, so there are some restrictions on it. Applet can't access system resources on the local computer. Applets are used to make the web site more dynamic and entertaining.
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:
init(): This method is called to initialized an applet
start(): This method is called after the initialization of the 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
Start () method:
() 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.
destroy() method: The destroy() method is called only one time in the life cycle of Applet like init() method. This method is called only on that time when the browser needs to Shut down.
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:
<APPLET ALIGN="CENTER" CODE="FirstApplet.class" WIDTH="800" HEIGHT="500"></APPLET>
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:
String strParameter = this.getParameter("Message");
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 :
Here is the code for the html program :
<TITLE>Passing Parameter in Java Applet</TITLE>
This is the applet:<P>
<APPLET code="appletParameter.class" width="800" height="100">
<PARAM name="message" value="Welcome in Passing parameter in java applet example.">
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.
Compile the program :
Output after running the program :
To run the program using appletviewer, go to command prompt and type appletviewer appletParameter.html Appletviewer will run the applet for you and and it should show output like Welcome in Passing parameter in java applet example. Alternatively you can also run this example from your favorite java enabled browser.