Applets

Dr Andy Evans

[Fullscreen]

Applets

  • Small programs that run on web clients.
  • Are contained in a tight security 'sandbox'.
    • Can't read or write files on the browser's machine.
    • Can't start local programs or access the local OS.
    • Can only access the machine it was sent from (though can get webpages from others).
    • Can't switch off and replace the JVM.
  • You can, however, "sign" applets. If the user trusts you the applets can be given more freedom.

Basics

  • Normal class code called by a line within a webpage which says that an applet should be inserted and where to find it.
  • All applets extend java.applet.Applet.
  • All therefore need to import java.applet.*.
  • java.applet.Applet is a subtype of Panel.
  • All applets are GUI based and all but simplest are Event Based.
  • Therefore they also need to import java.awt.* and java.awt.event.*. Note though, like panels, they can’t have menus in the webpage (though they can open Frames).

Methods

  • Don't usually have a 'main' method.
  • init()
    Replaces the main/constructor - used to set-up the applet. Called when applet first shown.
  • start()
    Used each time an applet is shown - e.g. if a webpage is visited, left and then revisited. Calls paint().
  • paint()
    Inherited by Applet from Panel / Component. Called at start and whenever needed, e.g. if the browser is covered by another window and then uncovered.

Methods

  • stop()
    Called when a browser leaves the webpage.
  • destroy()
    Called when the browser kills the applet - 'stop' is always called first. Not usually possible to say when this happens.
  • You don't have to implement all these methods - each has a default.
  • Just implement those you want - usually init, start and paint.

Basic Applet

	
import java.applet.*;
import java.awt.*
import java.awt.event.*

public class OurApplet extends Applet {

    public void init() {
        System.out.println("Hello World!");
    }

}

  • System.out is the browser's Java Console.

java.net.URL

  • To understand advanced Applet methods we need to understand URLs.
  • Encapsulates a Uniform Resource Locator (URL).
    http://www.w3.org:80/People/Berners-Lee/Overview.html
  • A URL represents another file on the network. It's comprised of...
    • A method for locating information - i.e. a transmission protocol, e.g. the HyperText Transmission Protocol (http)
    • A host machine name, e.g. www.w3.org
    • A path to a file on that server, e.g. /People/Berners-Lee/Overview.html
    • A port to connect to that server on, e.g. http connects to port 80

Client - Server architecture

  • A file requested using a URL will usually be handled by a client-server architecture.
  • Which server program gets which requests will depend on the port they are sent to, which is usually related to the transmission protocol.
  • e-mails are sent out to servers using Simple Mail Transfer Protocol (SMTP) which usually goes into Port 25.
  • One server can serve several clients at once.
  • Webservers use http/port 80 to listen for requests, then send out webpages from a particular requested directory.

Making a URL object

  • Need to import java.net.*, and deal with the MalformedURLException that the URL constructors throw.
  • You can then use one of the URL class constructors. Some let you put in the port, or split off the path.
  • Easiest is...
    URL url = new URL(String urlSpecifier);
    Where urlSpecifier is an address like...
    "http://www.w3.org/People/Berners-Lee/Overview.html"

Basic URL Methods

  • getFile()
    Gets the file name of this URL.
  • getHost()
    Gets the host name of this URL, if applicable.
  • getPath()
    Gets the path part of this URL.
  • getPort()
    Gets the port number (int) of this URL.

Advanced Applet methods

  • getImage(URL)
    Returns an java.awt.Image object from a given URL object.
  • getDocumentBase()
    Returns a URL object representing the directory the applet starting webpage was in.
  • Together with the URL methods, these methods allow us to retrieve image files from our web server.

Resource Streams

  • 	
    Class thisClass = getClass();
    URL url = thisClass.getResource("debts.txt");
    
    
  • As well as getting a URL Object this way, you can actually open a Stream to resources.
  • Class Objects have a getResourceAsStream method, which returns an InputStream.
  • This can be used to read the resource across the network or into a program from a local file.
  • More details on the Oracle site

java.net.URLConnection

  • If you want specific files from an http server which exists, it's easier to use a URLConnection object.
    	
    URL url = new URL("http://www.bbc.co.uk/eastenders/index.html");
    URLConnection urlc = url.openConnection();
    
    
  • Methods include...
    getDate() getContentLength() getInputStream()
  • Knowing the number of bytes of a resource (the ContentLength), and having an InputStream, you can now read the file.

Advanced Applet methods

  • getAppletContext()
    Returns an AppletContext object representing the environment the applet is running in.
    AppletContext ap = getAppletContext();
    The most important method in an AppletContext object is...
  • showDocument(URL webpage)
    This allows you to put a webpage into the browser window.
    ap.showDocument(new URL("http://www.w3.org/"));

Putting an applet into a webpage

	
<APPLET
	code = appletFile     (without the .class)
	width = widthInPixels
	height = heightInPixels
	align = left or right or center
>

<PARAM name="name1" value="attributeValue1">
<PARAM name="name2" value="attributeValue2">

</APPLET>

PARAMs

  • We can pull in the parameters using the applet's getParameter(String paramName) method, which returns the values associated with paramName as a String. For example, in our web page we might have:
    <PARAM name = "colorTypes" value = "1" >
    and in our init() we might have...
    	
    String colorScheme = getParameter("colorTypes");
    
    if (colorScheme.equals("1")) {
    	setBackground(Color.WHITE);
    }
    
    
  • Note that we treat "1" as a String, even though it is a numerical character.

Putting an applet into a webpage

	
<OBJECT
	codetype="application/java"	
	classid= appletFile 
	width = widthInPixels
	height = heightInPixels
>

<PARAM name="name1" value="attributeValue1">
<PARAM name="name2" value="attributeValue2">
</OBJECT>

Transferring files

  • When writing for the network, you can compress class files and associated resources using the jdk1.7/bin/jar.exe compressor. This creates "zip"-like files that can be read in java using java.util.zip or Class's getResource().
	
<APPLET  
	code = myClass
	archive = myJar.jar
	width = 300 height = 300>
</APPLET>

Web accessibility

  • If you are working for a public organisation, accessibility for the disabled has to be a major design driver. In addition, not everyone can see applets (~2% of browsers).
  • Generally you can make webpages accessible by not putting important information in images and sound.
  • Java 1.1 applets have no accessibility built in. Java Foundation Class / Swing applets have accessibility built in - will be usable with speaking browsers etc.

Web accessibility: Java 1.1

	
<APPLET code = appletFile width = widthInPixels height = heightInPixels>

Please visit this <A href="text.html">site</A> 
for a text list of our information.

</APPLET>

  • Anything you write between in the APPLET / OBJECT tags will only be displayed if the applet doesn't work.

Writing joint Applets and Applications

  • Because Applets are types of Panels, we can add them to a Frame.
	
public class TwoFaced extends java.applet.Applet {

    public void init() {
 		// All our code here.
    }

    public static void main(String[] args) {
		Frame appFrame = new Frame("Two Faced");
		TwoFaced twoFaced = new TwoFaced ();
		twoFaced.init();

		appFrame.add(twoFaced, BorderLayout.CENTER);
       	appFrame.setSize(300, 300);
 		appFrame.setVisible(true);    
    }  
}

Review

  • Applets are programs that run in web browsers.
  • Because of this they have their own security limitations.
  • They have init, start, stop, and destroy methods.
  • init and destroy are called when the applet is first started and killed respectively.
  • start and stop are called when the webpage is re-entered and left.

Review

  • Client - server architectures are a common mechanism for retrieving files over the internet.
  • One java class that uses them is URL which encapsulates a file location and the protocol to get it.
  • Applets can use URL objects to get files from the internet and display them.
  • Applets are usually run from webpages. The System.out is the Java Console.