BILKENT UNIVERSITY
CS533 Information Retrieval Systems
Technical Presentation Web Site
Barýþ UZ
 
Index of
this page
Introduction
Push Technologies
Channels
CGI
ASP
Java
Streaming Media
 
Overview
 Classes
Main Data Structures in Java
Object Oriented Programming
More about Object Oriented Programming
Applets
GUI
Images and audio
Multithreaded Programming
Links
 
 
Applets in HTML
People use Java not only because it is an object oriented language but also it allows running programs on the web. Thus, we use APPLETs for this purpose. 

An applet is an application that runs inside a web browser. It is a subclass of java.applet.Applet. 

We use <APPLET> and </APPLET> tags to embed Java Applets in our web pages.The <APPLET> tag is similar to the <IMG> tag. Like <IMG> <APPLET> references a source file that is not part of the HTML page on which it is embedded. IMGs do this with the SRC attribute. APPLETs do this with the CODE attribute. The CODE attribute tells the browser where to look for the compiled .class file. It is relative to the location of the source document. Thus, if you're browsing 

    http://www.cs.bilkent.edu.tr
and that page references an applet with  
    CODE="animation.class"
then the file animation.class should be at the URL 
    http://www.cs.bilkent.edu.tr/animation.class. 
For reasons that remain a mystery to HTML authors everywhere if the applet resides somewhere other than the same directory as the page it lives on, you don't just give a URL to its location. rather you point at the CODEBASE. The CODEBASE attribute is a URL that points at the directory where the .class file is. The CODE attribute is the name of the .class file itself. For instance if on the HTML page of the previous section you had written 
    <applet code="HelloWorldApplet.class" CODEBASE="classes"  width=200 height=200> 
    </APPLET> 
then the browser would have tried to find HelloWorldApplet.class in the classes directory in the same directory as the HTML page that included the applet. On the other hand if you had written  
    <applet code="HelloWorldApplet.class" 
    CODEBASE="http://www.foo.bar.com/classes" width=200 height=200> 
    </APPLET> 
then the browser would try to retrieve the applet from  
    http://www.foo.bar.com/classes/HelloWorldApplet.class 
regardless of where the HTML page was.  

In short the applet viewer will try to retrieve the applet from the URL given by the formula  

    (CODEBASE + "/" + code) 
Once this URL is formed all the usual rules about relative and absolute URLs apply. 

The HEIGHT and WIDTH attributes work exactly as they do with IMG, specifying how big a rectangle the browser should set aside for the applet. These numbers are specified in pixels and are required. 
 
Parameters
In case our Applets require command line parameters, we can achieve this by including <PARAM> tag in the referring HTML file: 

    <APPLET code="applet1.class" width="300" height="50"> 
    <PARAM name="Message" value="Initial message !"> 
    </APPLET> 
We should get this parameter in our Java program by using: 
    String inputFromPage = this.getParameter("Message"); 
 
Applet Capabilities
Although we can do a lot of job in Java, an applet is restricted to the following: 
    Write data on any of the host's disks. 

    Read any data from the host's disks without the user's permission. In some environments, notably Netscape, an applet cannot read data from the user's disks even with permission.  

    Delete files  

    Read from or write to arbitrary blocks of memory, even on a non-memory-protected operating system like the MacOS. 

    All memory access is strictly controlled.  

    Make a network connection to a host on the Internet other than the one from which it was downloaded.  

    Call the native API directly (though Java API calls may eventually lead back to native API calls).  

    Introduce a virus or trojan horse into the host system.  

    An applet is not supposed to be able to crash the host system. However in practice Java isn't quite stable enough to make this claim yet. 

It can 
    Draw pictures on a web page  

    Create a new window and draw in it.  

    Play sounds.  

    Receive input from the user through the keyboard or the mouse.  

    Make a network connection to the server from which it came and can send to and receive arbitrary data from that server.

 
Applet Life Cycle
All applets have the following four methods: 
    public void init(); 
    public void start(); 
    public void stop(); 
    public void destroy(); 
They have these methods because their superclass, java.applet.Applet, has these methods. 

In the superclass, these are simply do-nothing methods. For example, 

    public void init() {}
Subclasses may override these methods to accomplish certain tasks at certain times. 

The init() method is called exactly once in an applet's life, when the applet is first loaded. It's normally used to read PARAM tags, start downloading any other images or media files you need, and set up the user interface. Most applets have init() methods.  

The start() method is called at least once in an applet's life, when the applet is started or restarted. In some cases it may be called more than once. Many applets you write will not have explicit start() methods and will merely inherit one from their superclass. A start() method is often used to start any threads the applet will need while it runs. 

The stop() method is called at least once in an applet's life, when the browser leaves the page in which the applet is embedded. The applet's start() method will be called if at some later point the browser returns to the page containing the applet. In some cases the stop() method may be called multiple times in an applet's life. Many applets you write will not have explicit stop()methods and will merely inherit one from their superclass. Your applet should use the stop() 
method to pause any running threads. When your applet is stopped, it should not use any CPU cycles. 

The destroy() method is called exactly once in an applet's life, just before the browser unloads the applet. This method is generally used to perform any final clean-up. For example, an applet that stores state on the server might send some data back to the server before it's terminated. many applets will not have explicit destroy() methods and just inherit one from their superclass. 

For example, in a video applet, the init() method might draw the controls and start loading the video file. The start() method would wait until the file was loaded, and then start playing it. The stop() method would pause the video, but not rewind it. If the start() method were called again, the video would pick up where it left off; it would not start over from the beginning. However, if destroy() were called and then init(), the video would start over from the beginning.  

In the JDK's appletviewer, selecting the Restart menu item calls stop() and then start(). Selecting the Reload menu item calls stop(), destroy(), and init(), in that order. (Normally the byte codes will also be reloaded and the HTML file reread though Netscape has a problem with this.)  

The applet start() and stop() methods are not related to the similarly named methods in the 
java.lang.Thread class. 

Your own code may occasionally invoke start() and stop(). For example, it's customary to stop playing an animation when the user clicks the mouse in the applet and restart it when they click the mouse again.  

Your own code can also invoke init() and destroy(), but this is normally a bad idea. Only the environment should call init() and destroy().

 
PREVIOUS TOPIC
UP
(First page of this topic)
NEXT TOPIC
PREVIOUS PAGE 
(of this topic)
NEXT PAGE 
(of this topic)