You are on page 1of 3

Applet

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 remotel on the client browser! so there are some restrictions on it. Applet can"t access s stem resources on the local #omputer. Applets are used to ma$e the web site more d namic and entertaining. Advantages of Applet: Applets are cross plat%orm and can run on &indows! Mac '( and Linux plat%orm Applets can wor$ all the version o% Java )lugin Applets runs in a sandbox! so the user does not need to trust the code! so it can wor$ without securit approval Applets are supported b most web browsers Applets are cached in most web browsers! so will be *uic$ to load when returning to a web page +ser can also have %ull access to the machine i% user allows Disadvantages of Java Applet: Java plug,in is re*uired to run applet Java applet re*uires J-M so %irst time it ta$es signi%icant startup time I% applet is not alread cached in the machine! it will be downloaded %rom internet and will ta$e time Its di%%icult to desing and build good user inter%ace in applets compared to HTML technolog Life Cycle of Applet: Applet runs in the browser and its li%ec cle method are called b J-M when it is loaded and destro ed. Here are the li%ec cle methods o% an Applet. init(): This method is called to initialized an applet start(): This method is called a%ter the initialization o% the applet. stop(): This method can be called multiple times in the li%e c cle o% an Applet. destroy(): This method is called onl once in the li%e c cle o% the applet when applet is destro ed. init () method: The li%e c cle o% an applet begins when the applet is %irst loaded into the browser and called the init/0 method. The init/0 method is called onl one time in the li%e c cle on an applet. The init/0 method is basicall called to read the )A1AM tag in the html %ile. The init /0 method retrieve the passed parameter through the )A1AM tag o% html %ile using get )arameter/0 method All the initialization such as initialization o% variables and the objects li$e image! sound %ile are loaded in the init /0 method .A%ter the initialization o% the init/0 method user can interact with the Applet and mostl applet contains the init/0 method. Start () method: The start method o% an applet is called a%ter the initialization method init/0. This method ma be called multiple times when the Applet needs to be started or restarted. 2or 3xample i% the user wants to return to the Applet! in this situation the start Method/0 o% an Applet will be called b the web browser and the user will be bac$ on the applet. In the start method user can interact within the applet.

Stop () method: The stop/0 method can be called multiple times in the li%e c cle o% applet li$e the start /0 method. 'r should be called at least one time. There is onl miner di%%erence between the start/0 method and stop /0 method. 2or example the stop/0 method is called b the web browser on that time &hen the user leaves one applet to go another applet and the start/0 method is called on that time when the user wants to go bac$ into the %irst program or Applet. destroy() method: The destro /0 method is called onl one time in the li%e c cle o% Applet li$e init/0 method. This method is called onl on that time when the browser needs to (hut down. Creating First Applet E ample. 2irst o% all we will $now about the applet. An applet is a program written in java programming language and embedded within HTML page. It runs on the java enabled web browser such as 4etscape navigator or Internet 3xplorer. In this example ou will see! how to write an applet program. Java source o% applet is then compiled into java class %ile and we speci% the name o% class in the applet tag o% html page. The java enabled browser loads class %ile o% applet and run in its sandbox. !ere is the "ava code of program : import java.applet.56 import java.awt.56 public class 4ew#lass extends Applet7 (tring str6 public void init/07 str 8 9This is m %irst applet96 : public void paint/;raphics g07 g.draw(tring/str! <=!<=06 : : Here is the HTML code o% the program>HTML? >@'AB? >applet code89M applet9!height89C==9 width89C==9? >Dapplet? >D@'AB? >DHTML? 3xecution Method %or Applet . E. The applet vie#er command connects to the documents or resources designated b urls. It displa s each applet re%erenced b the documents in its own window. #.Fjavac? javac M applet.java #.Fjavac?appletviewer M applet.html

$assing $arameter in Java Applet. Java applet has the %eature o% retrieving the parameter values passed %rom the html page. (o! ou can pass the parameters %rom our html page to the applet embedded in our page. The param tag/>parma name899 value899?>Dparam?0 is used to

pass the parameters to an applet. 2or the illustration about the concept o% 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 %rom parameters. -alue o% a parameter passed to an applet can be retrieved using get)arameter/0 %unction. 3.g. code. (tring str)arameter 8 this.get)arameter/9Message906 $rinting the value: Then in the %unction paint /;raphics g0! we prints the parameter value to test the value passed %rom html page. Applet will displa 9HelloG Java Applet9 i% no parameter is passed to the applet else it will displa the value passed as parameter. In our case applet should displa 9&elcome in )assing parameter in java applet example.9 message. Here is the code %or the Java )rogram . import java.applet.56 import java.awt.56 public class applet)arameter extends Applet 7 private (tring strAe%ault 8 9HelloG Java Applet.96 public void paint/;raphics g0 7 (tring str)arameter 8 this.get)arameter/9Message906 i% /str)arameter 88 null0 str)arameter 8 strAe%ault6 g.draw(tring/str)arameter! <=! C<06 : : Here is the code %or the html program . >HTML? >H3AA? >TITL3?)assing )arameter in Java Applet>DTITL3? >DH3AA? >@'AB? This is the applet.>)? >A))L3T code89applet)arameter.class9 width89H==9 height89E==9? >)A1AM name89message9 value89&elcome in )assing parameter in java applet example.9? >DA))L3T? >D@'AB? >DHTML? There is the advantage that i% need to change the output then ou will have to change onl the value o% the param tag in html %ile not in java code. #ompile the program . javac applet)arameter.java 'utput a%ter running the program . To run the program using appletviewer! go to command prompt and t pe appletviewer applet)arameter.html Appletviewer will run the applet %or ou and and it should show output li$e &elcome in )assing parameter in java applet example. Alternativel ou can also run this example %rom our %avorite java enabled browser.

You might also like