Gopal Prasad Sharma
Lecturer, Purbanchal University, Biratnagar (Nepal)
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 remotely on the client browser, so there are some restrictions on it. Applet can't access system resources on the local computer. Applets are used to make the web site more dynamic and entertaining
Advantages of Applet:
- Applets are cross platform and can run on Windows, Mac OS and Linux platform
- Applets can work all the version of Java Plugin
- Applets runs in a sandbox, so the user does not need to trust the code, so it can work without security approval
- Applets are supported by most web browsers
- Applets are cached in most web browsers, so will be quick to load when returning to a web page
- User can also have full access to the machine if user allows
Disadvantages of Java Applet:
- Java plug-in is required to run applet
- Java applet requires JVM so first time it takes significant startup time
- If applet is not already cached in the machine, it will be downloaded from internet and will take time
- Its difficult to design and build good user interface in applets compared to HTML technology
The Life cycle of An Applet
In this Section you will learn about the lifecycle of an applet and different methods of an applet. Applet runs in the browser and its lifecycle method are called by JVM when it is loaded and destroyed.
Java Virtual Machine
Most compilers translate from the source language (eg, C or Pascal) into machine language for one specific type of machine (eg, Pentium or Sun). The machine language for a Sun computer is not the same as for a Macintosh, or a Windows machine. You cannot use the same machine code program on different kinds of machines.
Java compilers translate applets in two steps. The first part of the translation is from Java source to Java Virtual Machine (JVM) machine code. This may seem strange because there isn't any real machine that can use JVM instructions yet!
JVM code is designed so that it is easy to translate it into machine instructions for real machines, so the second part of the translation to real machine instructions is done in the browser on the user's machine. The browser takes the applet JVM code that it gets from the server and translates from JVM code to the machine code the browser is using, eg, a Pentium. This compiler in the browser is often called a "JIT compiler". JIT stands for "Just In Time", and it means that the last part of the compilation is done just before running the program. The diagram below shows how the same applet JVM code is used by browsers on different machines.
Two step compilation allows Java applets to run on many kinds of machines.
Here are the lifecycle methods of an Applet:
init(): This method is called to initialized an applet
start(): This method is called after the initialization of the applet. stop(): This method can be called multiple times in the life cycle of an applet. destroy(): This method is called only once in the life cycle of the applet when applet is destroyed.
init () method: The life cycle of an applet is begin on that time when the applet is first loaded into the browser and called the init() method. The init() method is called only one time in the life cycle on an applet. The init() method is basically called to read the PARAM tag in the html file. The init () method retrieve the passed parameter through the PARAM tag of html file using get Parameter() method All the initialization such as initialization of variables and the objects like image, sound file are loaded in the init () method .After the initialization of the init() method user can interact with the Applet and mostly applet contains the init() method.
Start () method:
The start method of an applet is called after the initialization method init(). This method may be called multiples time when the Applet needs to be started or restarted. For Example if the user wants to return to the Applet, in this situation the start Method() of an Applet will be called by the web browser and the user will be back on the applet. In the start method user can interact within the applet.
Stop () method: The stop() method can be called multiple times in the life cycle of applet like the start () method. Or should be called at least one time. There is only miner difference between the start() method and stop () method. For example the stop() method is called by the web browser on that time When the user leaves one applet to go another applet and the start() method is called on that time when the user wants to go back into the first program or Applet.
destroy() method:
The destroy() method is called only one time in the life cycle of Applet like init() method. This method is called only on that time when the browser needs to Shut down.
Applet Skeleton
import java.awt.*;
import java.applet.*;
/*
*/
public class AppletSkel extends Applet
{
//Called First
public void init()
{
// initialization
}
/* Called second, after init(). Also called whenever the applet is restarted.*/
public void start()
{
// start or resume execution
}
// Called when the applet is stopped.
public void stop()
{
// Suspends execution
}
/*Called when applet is terminated. This is last method executed.*/
public void destroy()
{
//perform shutdown activities
}
//Called when an applet’s window must be restored.
public void paint(Graphics g)
{
//redisplay contents of window
}
}
Order
When applet begins:
- init()
- start()
- paint()
When an applet is terminated
-
- stop()
- destroy()
Simple Applet Display Methods
- void drawstring(String message, int x, int y)
Sample Program
import
java.applet.*;
import
java.awt.*;
/* FirstApplet " width=300 height=200>*/
public class
FirstApplet extends
Applet
{
public void
paint(Graphics g)
{
g.drawString("Welcome in Java Applet.",40,20);
}
}
Or
We’re now ready to compile the applet. To do so, enter the command:
- Write code in Notepad
-
c:\\>javac FirstApplet.java(compile)
- c:\\>appletviewer FirstApplet.html(run)
Security Issues with the Applet
Java applet is run inside a web browser. But an applet is restricted in some areas, until it has been deemed trustworthy by the end user. The security restriction is provided for protecting the user by malicious code, like copy important information from the hard disk or deleting the files. Generally, applets are loaded from the Internet and they are prevented from: the writing and reading the files on client side. Some security issues to applet are following :
- Applets are loaded over the internet and they are prevented to make open network connection to any computer, except for the host, which provided the .class file. Because the html page come from the host or the host specified codebase parameter in the applet tag, with codebase taking precedence.
- They are also prevented from starting other programs on the client. That means any applet, which you visited, cannot start any rogue process on you computer. In UNIX, applets cannot start any exec or fork processes. Applets are not allowed to invoke any program to list the contents of your file system that means it cant invoke System.exit() function to terminate you web browser. And they are not allowed to manipulate the threads outside the applets own thread group.
- are loaded over the net. A web browser uses only one class loader that?s established at start up. Then the system class loader can not be overloaded, overridden, extended, replaced. Applet is not allowed to create the reference of their own class loader.
- They can’t load the libraries or define the native method calls. But if it can define native method calls then that would give the applet direct access to underlying computer.