Core Java - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Applet

Java Applet

shape Description

Java Applet is a client side program that runs with in a web browser or Applet viewer. An applet is a Windows-based program. Advantage of Applet is, it works on client side. So, less response time and Secured. Disadvantage is plugin is required at client browser to execute the applet. All applets are subclasses of the Applet class. Execution of Applet does not begin at the main() method. Java Applet are two types as follows. Output to your applet's is not performed by System.out.println(), it is handled with various AWT methods such as drawString().To use an Applet, it is specified in an HTML file. One way to do this is by using the APPLET tag. <applet code="Banner" width=300 height=50> </applet>

Difference between Applet and Servlets

shape Description

The following are the some differences between applets and servlets.

Applet

1)It is a special type of java program which runs on any web browser. 2)Applets resides at server but when client makes a request it completely loaded into a place in the browser that place is known as SAND BOX. 3)Applet lives inside the sand box only. it doesn't have any access to any things which exists out side of the sand box.

Servlets

1)Servlet is a purely server side program 2) Servlets always runs in the server. 3) Servlets can be placed in any web and application servers. For more detailed overview on servlets click here .

An Applet Skeleton

shape Description

Four methods init(), start(), stop(), destroy() apply to all applets and are defined by Applet. AWT-based applets will also override the paint() method,which is defined by the AWT Component class. This method is called when the applet's output must be redisplayed.

shape Examples

[java] package com.spl.applet; import java.applet.Applet; import java.awt.Graphics; public class AppletSkeleton extends Applet{ /*called when applet is terminated.This is the last method execution*/ @Override public void destroy() { // TODO Auto-generated method stub super.destroy(); //perform shutdown activities } //called first @Override public void init() { // TODO Auto-generated method stub super.init(); //initialization } /*called second,after init().Also called whenever the applet is restarted*/ @Override public void start() { // TODO Auto-generated method stub super.start(); //start or resume execution } //called when the applet is stopped @Override public void stop() { // TODO Auto-generated method stub super.stop(); //suspends execution } //called when an applet's window must be restored public void paint(Graphics g) { //redisplay contents of window } } [/java] When an applet begins,the following methods are called: 1)init(): The init() method is the first method to be called.This is where you should initialize variables.init() method is called only once during the run time of your applet. 2)start(): The start() method is called after init(). It is also called to restart an applet after it has been stopped. The init() is  called once, where as start() is called each time when an applet's HTML document  is displayed onscreen. 3)paint(): The paint() method is called each time your applet's output must be redrawn. The paint() method is also called when the applet begins execution.The paint() method has one parameter of type Graphics. When an Java Applet is terminated,the following methods are called 1)stop(): The stop() method is called when a web browser leaves the HTML document containing the applet when goes to another page. 2)destroy(): The destroy() method is called when an environment determines that your applet needs to be removed completely from memory. The stop() method is always called before destroy(). Output When compile the code following is the result will be displayed.

shape Example

Following is an another example. [java] package com.spl.applet; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; /* */ public class Banner extends Applet implements Runnable{ String msg="Moving Banner"; Thread t=null; int state; boolean stopFlag; //set colors and initialize thread @Override public void init() { // TODO Auto-generated method stub setBackground(Color.cyan); setForeground(Color.red); } //start thraed public void start() { t=new Thread(this); stopFlag=false; t.start(); } //Entry point for the thread that runs the banner @Override public void run() { // TODO Auto-generated method stub char ch; //display banner for(;;) { try { repaint(); Thread.sleep(250); ch=msg.charAt(0); msg=msg.substring(1,msg.length()); msg+=ch; if(stopFlag) break; } catch(InterruptedException e) { } } } //pause banner public void stop() { stopFlag=true; t=null; } //display the banner public void paint(Graphics g) { g.drawString(msg, 50, 30); } } [/java] Output: When compile the code result will be as follows.

Summary

shape Key Points

  • The stop() method is called when a web browser leaves the HTML document containing the applet when goes to another page.
  • When the Java Applet is terminated stop() and destroy() methods will be called.