The Java Applet - First step to web application development


java applet created revolution to web application development, life cycle includes init, start, stop and destroy. learn how to create applets in java, what is architecture and anotomy of java applet learn all the basics of applet in easy words.

If you are using web browsers and you have seen "loading Java" or "starting Java" message on your browser, we are sure that you have already met an Applet. The Applet is actually noting more than a window type environment which provides you a graphical interface to interact with the JVM. However this word is also commonly used by the Linux users to describe the small window screens opening up for multiple interface purpose in Linux.

The Java applet is much more than just a web page, but a fully capable software application which only works with your browser rest it takes the operation and its power from Java classes and runs on the JVM plug-in for browser. If you don't have JVM support for browser it would not be showing up but instead a "missing plug-in" or "plug-in required" message will appear. However you need not to worry. JDK ships with a special tool called "applet viewer" which is used for the testing and debugging of applet, however little bit of same HTML will be required here also.

When you are dealing with applet, you must know where they came from and why? In this article we will take you through the whole past life of applets and we'll also give you a very live and popular example of Java applet which is still being used my millions of users everyday. However let us just start with introduction and a brief history.

Emerging star of late 90s - The Applet

The Java was more than popular among so many existing programming language and world wide web was making people go crazy about the technology. Then sun Microsystems provided Java for Web. Yes, this was our new born applet. It was provided to take the application programming to a new level. To make not just an application but a web application. The applet provided people a very powerful application platform which was more than CGI scripts present at that time. But as they say, "problems are free with fame", our own applet was no exception. As applet was itself an application which could use your computer resources, hackers got a powerful tool to breakthrough your security. With the speed applet got its fame, same speed it fell down into darkness.

Since then, applet has gone through various changes, Applet could not make that reputation withing user community ever again, but it gave Java a start which no one could ever imagine. Java still provide support for applets however there are many other popular ways to work with web applications in Java. But here we are talking about Java applets only.

The Anatomy and Architecture of Applet

The applet is not a console based application. As We already told you, it is a web based program. When you load a web page you must embed an applet tag in your html to tell brower to send request to load an applet from the web server, and with the specification you provide the applet would be loaded and rendered on the browser. Again, it will only work if you have JVM browser support installed. Following is the structure of the applet tag for the web browsers.


<applet code="FirstApplet" width="400" height="400"></applet>

In the tag above the code tells about the Class-Name of the applet and width and height are obvious. So, all you need to do is to put this tag in your web file like this.


<!--it doesnot matters what name you give to this file it, just make sure it is a file type very well known by the browser, however if you are working with html like we are, we'll call it FirstApplet.html-->
<html>
<body>
<applet code="FirstApplet" width="400" height="400"></applet>
</body>
</html>

and you are good to load this applet if in case this applet exists. We will tell you more about creating an applet in a moment. Before that we want to tell you more about the working principles for applets.

The applets are actually based on Graphics and Events, and are different from the console based application, however you are still free to use the console for any purpose you like but when it comes to applets, console is generally and most of the cases only used for debugging purpose.

The events are the operation we perform, or in more technical terms whenever we interact with the computer system, we do it by providing some kind of user input that is an event and when system process that event and return an output corresponding to that event it is known as response. These events include our mouse clicking, mouse moving, mouse scroll, focusing on window, closing window, maximizing or minimizing window, pressing or releasing key or typing key and scrolling and resizing window etc. So applet provide us features for doing event based operation.

Applet is based on Java AWT(Abstract Window Toolkit) so it also helps us in doing graphical drawing and text processing in graphical way. We can create user interfaces which are friendlier to the user and easy to understand. But this will be in later part of the subject. However first let us describe the life cycle of the applet.

The Life Cycle of Applet

Human life cycle is divided in four phases, Birth, young age, old age and death. So, Applet seems inspired by this. Applet life is also divided in four phases init() start() stop() destroy()

The Init is initial or startup phase, you can say it act like a constructor, there is no main method in applet, and you would find that you neither need it. Also by mentioning that init() acts like constructor we don't mean that applets can't have constructor. They may have. However this is the matter of life cycle we'll discuss code in further part of this series.

start() method comes into play when a real working of applet starts. Everything is controlled by start method. When an applet is active the start is working.

stop() method is active when the applet is in minimized or is currently not active, there is no use for wasting resources when the applet is not in focus, that means user is not active on applet at the moment so its good to pause the working to save resources.

destroy() method clears up the resources if you are closing the applet, however this is not necessary as JVM manages this but just in case you want to do it yourself.

the flow between these four could go like this

init()----> start() -----> stop() -----> destroy()
		^------------'

That means application first initialize itself and than goes to start mode, from start it may go to either destroy or it may come back again to the start mode. this is a small and sensible life of an applet.

Now that we know quite a much about the applet let us show you how you are going to create your first applet.

My first Applet

Its time to open up an editor and write some code. The Applet is actually a classes defining all the methods you need for the life cycle along with utility method. However in this post only life cycle methods will be discussed. We are planning to provide you more details in coming articles. So, as I told you Applet is a predefined class, you all you need to do is to make it a super class for your applet class. Follow these steps and you are ready to play in applets.

  • import applet class into your program
  • make Applet class as super class(I do believe that you may find out other methods instead of extending Applet but its good to stick to conventions).
  • override life cycle methods and
  • add the applet tag in code or create a calling html with applet tag(if you are working with the applet viewer you must add the applet tag in the comments within the class file).

import java.applet.Applet;

public class FirstApplet extends Applet{
	public void init(){
		System.out.println("initialized");
	}

	public void start(){
		System.out.println("started");
	}
	
	public void stop(){
		System.out.println("stop");
	}

	public void destroy(){
		System.out.println("destroyed");
	}

}

/*<applet code="FirstApplet" height="400" width="400"></applet>
*/

The compilation will be regular but invocation will be little different. To run you need to call the file with the applet tag. In our case we have added tag in the same file comment so this wile will do the trick. Use following command to run the code.

appletviewer FirstApplet.java

Note: you need to call the file with the tag not the class file. it will run the applet viewer and will display the output like this.

applet

The top shows the title with the Name of applet class. Than we have a default "Applet Menu" which have different applet related menus and the middle is the content area where you can do do anything. Finally at bottom is the status bar which shows the status of the applet.

Note: we have used System.out.println() with some of the messages in it, we leave it on you to try different operations like minimizing and maximizing and closing and observe the result on the console.

An awesome example of Java Applet programming is KeepVid. This website helps you to download youtube videos in various formats and this happens with the help of Applets.

If you have learned the basics now its time to move on to some more utilities of applets here. Java Applet Programming - playing with various utilities.