CS102: Applets, and URL Connections

Copyright © 1999, Kenneth J. Goldman

Streams and URL Connections

Last time, we saw input stream and output stream as abstractions that are useful when we think about reading or writing files. However, the idea of a stream is not unique to files. A stream is really a kind of a queue -- data is put into one end and read from the other end.

When streams are used for files:

Input stream -- the filesystem is upstream and the application is downstream

Output stream -- the other way around

The same abstraction can be used any time a sequence of data is to be moved from one place to another. For example,

A Web server can be upstream and a Web browser (or an applet in a Web browser) can be on the downstream end.

By using the same abstraction for many forms of communication, the same code can be reused and the programmer's job is simplified. For example, the stream tokenizer can be used to parse any input stream, whether its source is a file, a Web server, or some other application writing to an output stream (more on that later).

Let's see in example for the Web server case. In this applet we'll write a method to return to stream tokenizer for a given URL.

import java.applet.*;
import java.net.*;
import java.io.*;
public class EasyAsPie extends Applet{
public StreamToekenizer getTokenizerForURL(String URLname) throws IOexception{
URL url = new URL(URLname);
URLconnection con = url.openConnection();
InputStream is = con.getInputStream();
return new StreamTokenizer(is);
Or you can do the above a much shorter way:

return new StreamTokenizer((newURL(URLname)).openStream());

Once the StreamTokenizer is returned, the rest of the code need not be concerned with the fact that the data happens to be coming from a web server. That's all hidden below the abstraction!

Note that when you open a URL for an Applet inside a browser, this is not the same as asking the browser to display a document at a given URL. In the former case, your executing program has access to the data, whereas in the latter case, it is the browser that accesses the data and displays it.

However, from within an Applet, you can ask the browser to display a document by giving a URL and optionally specifying a target frame. This is done as follows:

URL url = new URl(URLname);
AppletContext ac = getAppletContext(); //inherited from Applet

ac.shotDocument(url,target); // target is a string that is the name of the target frame where the document should be displayed.


To this point in the course, we've been using Java to write a variety of applications.

When we write an application, we compile the .java files into bytecode files (.class files). Then we run a Java interpreter that loads and executes the bytes code, carrying out each of our instructions.

Well, it turns out that web browsers, like Netscape, have built-in Java interpreters, which means that the interpreter in the above diagram could actually be inside a web browser.

This allows you to put Java programs on a web site so people browsing the web site can interact with those programs in their web browsers. These programs are called applets.

Bird's-eye View

When you type a URL into your browser,
  1. the broser sends a request to the named web server
  2. the web server responds by sending the document
  3. the browser processes the document, which may contian more URLs for images and other items that are part of that web page
  4. the browser sends requests to the server for the items needed to display the page.
    One (or more) of these might be a Java applet.
  5. The server responds by sending each of the requested items.
  6. For each item recieved, the browser displays it according to its type. If it's a Java program, the browser displays it by running the Java interpreter.

Applets vs. Applications
  1. Applets may run in a web browser
  2. Applets tend to be graphics-based(no terminal input)
  3. Their classes are publicly available (can be limited) so the browser can get them
  4. There are security restrictions on what applets can do
    (For example, applications can write to your local files, but applets can't.)
  5. Are implemented as a subclass of Applet

How the applet and the browser interact
  • First, look at an example of an HTML file containing an applet.
    <TITLE>Ken's Applet</TITLE>
    <H1>Here's my applet...</H1>
    <Applet code=MyApplet.class width=640 height=480>
    <PARAM name="Messge" value="Welcome to CS101!">

    What the browser does:
    When it sees <APPLET..>
    it makes space on the display with the given dimensions

    it sends a request to the web server for the code, assumed to be in the same folder as the html page (more about this later)

    it sets up a parameter list

    it initializes, starts, and paints the applet

  • Now, look at an example of the applet itself:
    import java.applet.Applet;
    import java.awt.*;

    public class MyApplet extends Applet{
    String announcement;
    int x,y;
    public void init(){
    announcement = getParameter("Message"); x = bounds().width/2;
    y = bounds().height/2;
    public void paint(Graphics g){

    Things to notice:
    • No constructor (inherited from Applet)
    • getParameter is used to find PARAM values specified in the HTML file
    • bounds() is a method on Applet that returns a Rectangle

Methods a subclass of Applet may override:

public void init() -- called once by the browser when applet if first loaded. used to perform initialization.

public void start() -- called whe the applet is first loaded and whenever the browser regurns to the page.

public void stop() -- called when applet should stop executing (when browser leaves the page) performing necessary tasks to suspend.

public void print(Graphics g) -- called when browser need to repaint the applet.

public void destroy() -- called when applet is being removed from memory (usually used to terminate threads)


Note the following html code:

<applet codebase=javadir code=MyApplet.class>>

The codebase section here tells the browser the path where the code is. In the applet, we may want to know the URL of the code base in order to, for example, load images from that directory for display in applet:

import java.net.*;
URL codebase;
codebase = getCodeBase();