Applet is introduce by Sun Microsystems in 1995. An Applet is the special kind of Java programming which typically embedded inside a web page and runs in the context of a browser.
The Applet class is contained in the java.applet package, contain several method that provide detailed control over the execution of your applet.
The Applet class provides the standard interface between the applet and the browser environment, in addition java.applet also define the three main interface
- AppletContext
- AudioClip
- AppletStub
Since, Java applet is deliver to the user in the form of java's bytecode, so it is platform independent. Java applets can run in a Web browser using a Java Virtual Machine (JVM), or in Sun's AppletViewer( i.e. a stand-alone tool for testing applets), usually written in java programming language but can also written in another language that enable to compile Java bytecode such as JRuby, Jython or SmartEiffel.
All the applet's class must implement java.applet package. Applet also import from java.awt package, AWT stand for Abstract Window Toolkit.
Since Applet also stand along application but it don't need main() method for execution and doesn't support system,out,print(); Rather it is handle various AWT method like JButton, JLevel, etc also handle with various location by drawString() method, which outputs a string to a specified X,Y location.
- init() method- use for initialized and load the applet into applet viewer or browser.
- start() method- use for start the applet in applet viewer or browser.
- stop() method- use for stop the running applet.
- destroy() method- use for final clean up or unload the the applet.
Note:
That when webpage is refresh or reload the current instance of applet is stop and destroy( by calling stop() and destroy() method itself), after that a new instance of same applet is generated automatically.Use the following program:l/* * Save as a FirstApplet.java * Program define complete Life cycle of applet. */ package r4r.co.in; import java.awt.*; public class FirstApplet extends java.applet.Applet { //define a private label private Label label; /* * Initialization method that will be called after the applet is loaded * into the browser. */ public void init() { setBackground(Color.orange); } public void start() { /* * Start method is call after the applet is completely load * inside a AppletViewer/Browser */ label = new Label("R4R Tech Soft! Welcome:"); add(label); } public void stop() { //Stop method is call when close button is click } public void destroy() { //Method call After Stop method for Finally unload the applet } public void paint(Graphics g) { /* *Method used for providing Graphic in applet. * Call at the run time */ label.update(g); } }
Embedded Applet in HTML page
Use the following syntax for such operation.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>R4R Tech Soft</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <!-- Use the following syntax for Embedded an applet into a HTML page name= Name of Applet code=Provide the path of .class file of the applet( not .java file) height && width= provide the context area of applet in HTML page --> <applet name="CommApplet1" code="r4r.co.in.CommApplet1.class" height="200" width="200" /> </body> </html>
AppletStub Interface
When an applet is first created, an applet stub is attached to it using the applet's setStub method because stub serves as the interface between the applet and the browser or applet viewer in which the application is running. AppletStub interface contain some of method like-
- void appletResize(int width, int height): used to when the applet wants to be resized.
- getAppletContext(): use to handler the applet's context.
- getParameter(String name): use to get the named/string parameter from the HTML/Field tag.
- isActive(): use to determines the state of applet.
- getCodeBase(): use to gets the URL of applet.
- getDocumentBase(): use to gets the URL of the document in which the applet is embedded.
Applet have some limitation such as
- Since, applet is the platform independent but for running condition it need Java Virtual machine (JVM) or sun's AppletViewer.
- Across internet, it is embedded into HTML page, very bulk process.
- Applets executes under security limitations( sandbox security model) that disallow certain operation and divide into two category-trusted and untrusted applet.