2. Applet
● Applets are small Java applications that can be accessed on an Internet server,
transported over Internet, and can be automatically installed and run as a part of a
web document.
● Applet is a special type of program that is embedded in the webpage to generate
the dynamic content.
● It runs inside the browser and works at client side.
● An applet can be a fully functional Java application
● After a user receives an applet, the applet can produce a graphical user interface.
● It has limited access to resources so that it can run complex computations without
introducing the risk of viruses or breaching data integrity.
3. Applet Cont...
Advantage of Applet
● It works at client side so less response time.
● Secured
● It can be executed by browsers running under many platforms, including Linux,
Windows, Mac Os etc.
● It takes very less response time as it works on the client side.
● It can be run on any browser which has JVM running in it.
Drawback of Applet
● Plugin is required at client browser to execute applet.
4. Applet Cont...
● Applets are used to make the web site more dynamic and entertaining.
Some important points :
1. All applets are sub-classes of java.applet.Applet class.
2. Applets are not stand-alone programs. Instead, they run within either a web
browser or an applet viewer. JDK provides a standard applet viewer tool called
applet viewer.
3. In general, execution of an applet does not begin at main() method.An applet
class will not define main().
4. Output of an applet window is not performed by System.out.println(). A JVM is
required to view an applet. The JVM can be either a plug-in of the Web
browser or a separate runtime environment.
8. Applet Life Cycle Cont...
● When an applet begins, the following methods are called, in this sequence:
1. init( )
2. start( )
3. paint( )
● When an applet is terminated, the following sequence of method calls takes place:
1. stop( )
2. destroy( )
9. Applet Life Cycle Cont...
1.public void init( ) : The init( ) method is the first method to be called.
● This is where you should initialize variables. This method is called only once
during the run time of your applet.
2. public void start( ) : The start( ) method is called after init( ).
● It is also called to restart an applet after it has been stopped.
● Note that init( ) is called once i.e. when the first time an applet is loaded
whereas start( ) is called each time an applet’s HTML document is displayed
onscreen.
● So, if a user leaves a web page and comes back, the applet resumes
execution at start( ).
10. Applet Life Cycle Cont...
3. public void paint( ) : java.awt.Component class provides 1 life cycle method of
Applet.
● is used to paint the Applet.
● It provides Graphics class object that can be used for drawing oval, rectangle, arc
etc.
● paint( ) is also called when the applet begins execution. Whatever the cause,
whenever the applet must redraw its output, paint( ) is called.
11. Applet Life Cycle Cont...
4. public void stop( ) : The stop( ) method is called when a web browser leaves the
HTML document containing the applet—when it goes to another page
● When stop( ) is called, the applet is probably running.
● You should use stop( ) to suspend threads that don’t need to run when the applet
is not visible.
5. public void destroy( ) : The destroy( ) method is called when the environment
determines that our applet needs to be removed completely from memory.
● At this point, we should free up any resources the applet may be using.
● The stop( ) method is always called before destroy( ).
12. The HTML APPLET Tag
● The HTML <applet> tag specifies an applet.
● It is used for embedding a Java applet within an HTML document.
● It is not supported in HTML5.
Passing Parameters to Applets
● 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(<param name="" value=""></param>) is used to pass the
parameters to an applet.
● The applet has to call the getParameter() method supplied by the
java.applet.Applet parent class.
13. Passing Parameters to Applets Cont...
Here are three methods commonly used by applets:
● String getParameter(String name)-Returns the value for the specified parameter
string
● URL getCodeBase()-Returns the URL of the applet
● URL getDocumentBase()-Returns the URL of the document containing the applet
Using the Status Window
● In addition to displaying information in its window, an applet can also output a
message to the status window of the browser or applet viewer on which it is
running.
● To do so, call showStatus( ) with the string that you want displayed.
14. Using the Status Window
● The status window is a good place to give the user feedback about what is
occurring in the applet, suggest options, or possibly report some types of errors.
● The status window also makes an excellent debugging aid, because it gives you
an easy way to output information about your applet.