13 Applet API

Pravin Jain

epgp books

 

Applet Context

 

An instance of “AppletContext” is used for interaction with the browser. The instance of “AppletContext” is available to the Applet from the “AppletStub” instance. The  methods in the “AppletContext” can be used to locate other applets in the Web page, load image and audio resources from any specified URL, show any document in the browser by specifying its URL, set messages in the status bar of the browser, etc.

 

The “AppletContext” has methods to get access to the applet instances in the corresponding web page as:-

  • Enumeration “getApplets()”    // returns an Enumeration containing all the applet instances on the corresponding web page
  • Applet getApplet(String name)  // return the applet instance with the name as its id.

The method “getApplets()” returns an Enumeration containing all the applets which are loaded in the current Web page. The method “getApplet()” can be used to fetch an instance of the Applet by its name, which has been specified in the Web page.

 

The “showMessage()” method can be used to set a message on the status bar of the browser. The “showMessage()” method has a string parameter to specify the message to be set on the status bar as given in the listing below:-

  • void showStatus(String msg)

What is a URL class? 

 

The URL class(defined in the “java.net package”) encapsulates the information within a URL. what are the different parts of a typical url. Lets take a typical url.

 

http://forum.java.sun.com/forum.jspa?forumID=24

<protocol>://<host>/<file>?<querystring>

 

so a url has protocol, host, port, file, query string or reference, a reference is mentioned as “#” followed by a reference within the file.

 

An instance of URL could be used to parse all the information in a URL string you can create an instance of URL by calling the constructor with string as parameter

  • URL(Sring urlstring)                                    // url string can be any kind of url.
  • URL(URL location, String s)                     // url created relative to an existing URL.

There are other constructor’s where you can create instance of URL by specifying all the parts of the URL.

 

Now coming to the AppletContext instance, if the applet wants to interact with the browser, it can invoke methods on the AppletContext instance available to it thru the AppletStub, which was given to it in the setStub method. The methods in the AppletContext are:

 

public void showStatus(String msg)                // show a message on the status bar of the browser.

public void showDocument(URL webpage, String target)          // load the webpage and show on  the

target. as mentioned in the API.

 

The main activity of a browser is to load resources from a give URL. These resources could be webpage or some other content type like images and audio. In “java.awt.” there is a class called Image. which can be used for rendering on any Component by using the “drawImage” method of Graphics class. The “AppletContext” has methods to create an instance of Image from a give URL location on the web. It has methods like:

 

Image getImage(URL imagefile)            //downloads the content of image file from the URL

// and creates an instance of Image from the content.

 

AudioClip getAudioClip(URL audiofile)           //downloads the content of audio file from

//the URL and creates an instance of Audioclip

//from the content.

 

Applet Class 

 

Now if we look at the methods of the Applet class. It has inherited methods from the “Panel”, and additional methods are the life cycle methods plus the methods which would in turn interact with the “AppletStub” or “AppletContext”. Most of these methods are the same as methods in either “AppletStub” or ”AppletContext”.

 

Methods from the stub 

 

String getParameter(String name) URL getCodeBase()

URL getDocumentBase()

void resize(int w, int h)       // this would in turn invoke appletResized on the

//corresponding stub

void resize(Dimension d) AppletContext getAppletContext() boolean isActive()

 

Invoking any of the above methods on an instance of Applet results in the invocation of the corresponding method on the stub. The “resize()” method is not available in the “AppletStub”. The invocation of “resize()” method on the Applet results in the invocation of “appletResize()” method on the stub.

Methods from the AppletContext 

 

void showStatus(String msg) Image getImage(URL iamgefile)

Image getImage(URL location, String imagefile)             // imagefile relative to the location

AudioClip getAudioClip(URL audioFile)

AudioClip getAudioClip(URL location, String audioFile)

void play(URL u) //gets the AudioClip from the url and calls play on the AudioClip

void play(URL u, String audiofile)

 

Invoking any of the above methods on an instance of Applet results in the invocation of the corresponding method on the “AppletContext” instance which is available within the stub. The invocation of “play()” method on an instance of Applet results in the invocation of the “getAudioClip()” method on the “AppletContext” and then the invocation of “play()” method on the “AudioClip” instance.

 

Additionally, the Applet has methods which are informative, ie. they may be overridden to provide into about the applet and its parameters.

 

String getAppletInfo()                      // u may return author name, purpose etc.

String[][] getParametersInfo()         // you may return a 2-dimension string array

// describing info about the parameters which this applet may look from

// the web page in the param tag

 

The “getAppletInfo()” method is used to return information about the applet, usually overridden to return a copyright message for the applet, and the method “getParameterInfo()” is used to return information about the parameters which this applet can understand, when specified in the applet tag. The return type for this method is a two- dimensional array of string, where we have an array for each parameter for each parameter, with three elements. The first element is the parameter name, second is the parameter type and the third is used for information about the parameter.

 

These is another method which may give information about the Locale of the client, ie. the machine on which the browser is running. The instance of Locale typically encapsulates information about the Language and  country settings. so you also have  a method in the Applet class called Locale “getLocale()”.

 

Audio Clip 

 

The AudioClip interface whose instance would be created when an audiofile is downloaded by the browser has following methods:

void play()

void loop()

void stop()

The “play()” method will play the audio only once, whereas the method loop() results in a repeatedly playing of the audio. The “stop()” method will stop the audio which is currently being played using the AudioClip instance.

 

So Applet is a GUI container with capability to interact with the web browser in which it has been enbedded.

 

Security Issue 

 

Now if we look at the way an applet is loaded, we find that the applet can be loaded whenever a user loads a webpage in his browser. The user when loading the webpage may not have the idea whether the page contains an applet or not applet is a java code which runs inside the users browser, without the knowledge of the user. ie. The user may not specifically invoke the applet code. This could lead to some security issues. The security from applets is the responsibility of the browser. The java application which is started initially uses a ”ecurityManager” to ensure that the applet does not get access to critical resources on the local machine. It would ensure that the applet is not allowed to:

 

read, write, delete or create files on the local file system.

 

It cannot open a socket connection to any host other than the host from where it was loaded. it cannot load libraries, cannot terminate the jvm (System.exit()).

 

As a Container the Applet extends from Panel and so by default it has the “FlowLayout” as the default “LayoutManager”.

 

The GUI components which are used in an Applet should be the components from the “java.awt” package. In order to use Swing components in an applet, we can use the “JApplet” class, which is a sub-class of the Applet class. The “JApplet” in the “javax.swing” has one typical feature. ie. it can have Menus. The “JApplet” has most of the capabilities of “JFrame”.

 

In Summary: 

  • Most of the methods in the Applet use the instance of “AppletStub” and “AppletContext” available from the “AppletStub”. These methods can be used for fetching parameter values from the Web page associated with the applet. Fetching Image or “AudioClip” instances for a given URL, etc.
  • The applets are normally restricted by the browser from accessing the local file system and network; it is also restricted from terminating the JVM. This is to protect the applet from being used to breach the security of the data on the client’s machine. The security is the responsibility of the browser, which uses a security manager to manage the restrictions for the applet.
  • The “JApplet” is a Swing version of the applet and has the capability of using menus.
you can view video on Applet API
Suggested Reading:
1. Core Java Volume 2 by Cay Horstmann & Gary Cornell, Ninth Edition, Pearson Education.
2. The class of JAVA by Pravin Jain, Pearson Education.
3. An Introduction to Programming with Java Applets by Elizabeth S. Boese
4. https://docs.oracle.com/javase/tutorial/deployment/applet/index.html