Java Applet
From CS315
A Java applet is a type of software program that Java or browsers that include a Java Virtual Machine (JVM) can download and run, and it was developed by a team at Sun Microsystems. Sun Microsystems also developed a stand alone tool for developing and testing java applets, called AppletViewer, it is also obviously able to run java applets. Java Applets are usually used to add features to web pages that HTML or JavaScript cannot. They are most commonly written in the Java Programming Language, but can also be written in other languages that compile to Java bytecode.
Contents |
History
Java, first bound for the cable TV business, didn’t get the feedback it hoped from the executives at the cable companies, so, it was introduced somewhere else, the Internet. It was first introduced in early 1995 at the Technology, Entertainment and Design Conference in Monterey by John Gage, director of Sun’s Science Office and James Gosling, one of the original creators of Java. At first, the internet was a completely text based, un-interactive world. With the introduction of Java, it could bring the internet to life with moving images and neat animations, needless to say it was a hit at the conference. This made people rethink the potential of the internet and played a big part in making the internet what it is today.
Code Examples
This is a typical “Hello World” program example done in an applet form. When this program executes, it simply displays the text “Hello World”. As you can see, in addition to the standards of a normal Java program, it requires import statements and special methods rather than the usual “Main” method, which is non-existent. Some of those special methods that are used within applets are below in the Technical Information section.
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}
}
There are also some much more complex code examples provided through Sun Microsystems at the website below.
http://java.sun.com/applets/jdk/1.4/index.html
Technical Information
The code of the java applet is downloaded from a web server and the browser either embeds the applet into a web page or opens a new window to display the applet. Java applets can be displayed within a web page by using the applet tags or the object tags. This specifies the applet's source and the applet's location.
As you can see from the example, Java applets extend the Applet class. The class must override methods from the extended class in order to set up a user interface inside itself. Below are a few methods and their responsibilities.
- public void init()
- This method will be automatically called when the applet is started, nothing is required to go inside, but this is a good place to specify size, and other constructor type conditions.
- public void stop()
- This method gets called when the applet is terminated, for example, when the user goes to another page or exits out of their browser.
- public void paint(Graphics g)
- This is the method that is used primarily to "paint" on the applet window, this is also the method I used in the Hello World example. This overrides the empty Applet method, you can't call it "display" for example.
I have provided a link below to the Graphics class, this will give you an idea of what you can do inside the paint method, because you are given a Graphics object as a parameter.
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html
Below is the link to the Applet Class, which includes the methods that you gain access to, and can perform when writing your own applet code.
http://java.sun.com/j2se/1.5.0/docs/api/java/applet/Applet.html
Tutorials
Here I’ve provided a few links for those of you interested in learning how to use java applets.
Security
When Java was first introduced, the client side java applet was very widely used, but due to the fact they are run through a browser, they can encounter some risky code on the internet, so they are run in a Sandbox with very limited permissions to protect the user. Applet developers protested that the Sandbox was too restrictive and took away from the possibilities of the applet, therefore, we now have the very pesky certificates we must “sign”, in order to determine if an applet will gain full permission, or be denied by the user. Usually we have a window pop up before the applet can be loaded saying something similar to “The application’s digital signature cannot be verified. Do you want to run the application?” or something along the lines of “Are you sure you want to run this software?”. So really think before you click that “Run” button to be sure you aren’t downloading some risky and mischievous applet code to run on your system. There are plenty of hostile applets out there, and it has been that way for quite some time. Take a look at these articles dated February 2, 1997, and April 22, 1998, it didn't take long for hackers to start exploiting applets.
Advantages
Advantages of java applets:
- it can cross platforms extremely easily, making it a versatile solution for any operating system
- it is supported by almost all web browsers
- it will cache (save the code) in most web browsers, so will be quick to load when returning to a web page
- it can have full access to the machine it is running on if the user agrees
- it can run at a comparable speed to other compiled languages such as C++
- it decreases the workload on the servers, by shifting it to the client thus making the server a more efficient web solution.
Disadvantages
Disadvantages of java applets:
- it requires the browser have a Java Virtual Machine to compile and run the code.
- it may sometimes have a very long start time because of the fact the Java Virtual Machine must start before it can start processing the applet's code.
- if it is labeled untrusted, it has little access to the user’s machine.
- If on a machine that is run by an administrator that is not you, installations can be limited to only those applications the admin has provided, and that will sometimes not include the tools necessary to run java applets.
- depending on the applet, it might require a specific version of the JRE, which can make it unavailable to many users.
- the security of these applets has been exploited in some cases, causing end users to be attacked through them.
Compatibility Issues
Netscape has had some problems with the height of the applet being generated, and in turn, it not being the size that the creator wishes. This causes some mild grief, as the applet can be prematurely terminated if the user accidentally clicks outside of the applet window, and because of the size problems, this was a concern by many.
Similar Technology
Some other various tools are available to capture some of the possibilities that are possible with applets. These tools include:
- DHTML
- Microsoft Silverlight
- Adobe Flash
