Saturday, June 18, 2011

Just the basic of Java Applet

Java Applet


-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 desing and build good user interface in applets compared to HTML technology


-What Is Java Plug-in?
Java Plug-in extends the functionality of a web browser, allowing applets or Java Beans to be run under Sun's Jave 2 runtime environment (JRE) rather than the Java runtime  environment that comes with the web browser. Java Plug-in is part of Sun's JRE and is installed with it when the JRE is installed on a computer. It works with both Netscape and  Internet Explorer.
This functionality can be achieved in two different ways:
1.By using the conventional APPLET tag in a web page.
2.By replacing the APPLET tag with the OBJECT tag for Internet Explorer; by replacing the APPLET tag with the EMBED tag for Netscape 4. Note, however, that the OBJECT and EMBED tags must conform to a special format as described in the next chapter, Using OBJECT, EMBED and APPLET Tags in Java Plug-in.


-Applet versus Application:
      Applets as previously described, are the small programs while applications are larger programs. Applets don't have the main method while in an application execution starts with the main method.Applets are designed just for handling the client site problems. while the java applications are designed to work with the client as well as server. Applets are created by extending the java.applet.To create an applet just create a class that extends the java.applet.Applet class and inherit all the features available in the parent class.
import java.awt.*;
import java.applet.*;
class Myclass extends Applet {
public void init() {
/* All the variables, methods and images initialize here will be called only once because this method is called only   once when the applet is first initializes */
}
public void start() {
/* The components needed to be initialize more than once in your applet are written here or if the reader switches back and forth in the applets. This method can be called more than once.*/
}
public void stop() {
/* This method is the counterpart to start(). The code, used to stop the execution is written here*/
}
public void destroy() {
/* This method contains the code that result in to release the resources to the applet before it is finished. This method is called only once. */
}
public void paint(Graphics g) {
/* Write the code in this method to draw, write, or color things on the applet pane are */
}
}


In the above applet you have seen that there are five methods. In which two ( init() and destroy ) are called only once while remaining three (start() , stop() , and paint() ) can be called any number of times as per the requirements. The major difference between the two (applet and application) is that java applications are designed to work under the homogenous and more secure areas. On contrary to that, java applets are designed to run the heterogeneous and probably unsecured environment. Internet has imposed several restrictions on it.
Applets are not capable of reading and writing the user's file system. This means that the applet neither can access nor place anything locally. To illustrate this lets take an example.. Many Window based C applications uses the .INF file as the initialization file to store the information about the application and any user preferences in 16-bit Windows or the Registry in 32-bit Windows. While in case of current applet it is not possible. One more thing to point here is that applets are unable to use the native methods, run any program on the user system or load shared libraries. The major security concern here is  that the local shared libraries and the native methods may results in the loophole in the java security model.
Applets are not capable of  communicating the server than one from which they are originating. There are the cases in which an encryption key is used for the verification purpose for a particular applet to a server. But accessing a remote server is not possible.
The conclusion is that the java applets provides a wide variety of formats for program execution and a very tight security model on the open environment as on the Internet.


-The Life cycle of An Applet:
Applet runs in the browser and its lifecycle method are called by JVM when it is loaded and destroyed. 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.
HTML provides a tag that enables the developer  to "embed" the applet within the page. This tag is known as the APPLET tag.

No comments:

Post a Comment

MVC - MVP : Difference between these design patterns?

In traditional UI development - developer used to create a  View  using window or usercontrol or page and then write all logical code ...