Java Applets and Applications

Applet in Java

  • Applets are small Java applications that can be accessed on an Internet server, transported over Internet, and can be automatically installed and run as apart of a web document. Any applet in Java is a class that extends the java.applet.Applet class.
  • An Applet class does not have any main() method.
  • It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a separate runtime environment to run an applet application.
  • JVM creates an instance of the applet class and invokes init() method to initialize an Applet.

A Simple Applet

import java.awt.*;
import java.applet.*;
public class Simple extends Applet
{
 public void paint(Graphics g)
 {
  g.drawString("A simple Applet", 20, 20);
 }
}
creating simple applet
Every Applet application must declare a paint() method. This method is defined by AWT class and must be overridden by the applet. paint() method is called each time an applet neede to redisplay its output. Another important thing to notice about applet application is that, execution of an applet does not begin atmain() method. In fact an applet application does not have any main() method.

Advantages of Applets

  1. Very less response time as it works on the client side.
  2. Can be run using any browser, which has JVM running in it.
Application
Application is a Java class that has a main() method. An applet is a Java class which extends java.applet.Applet. Generally, application is a stand-alone program, normally launched from the command line, and which has unrestricted access to the host system. An applet is a program which is run in the context of an applet viewer or web browser, and which has strictly limited access to the host system. For instance, an applet can normally not read or write files on the host system whereas an application normally can. The actions of both applets and applications can be controlled by SecurityManager objects.

1 comment: