Java and the Internet: Manipulating your Browser

 The next easiest thing in terms of Java's network features is - perhaps surprisingly - to get the browser inside which an applet is running to load and display a web page from the world-wide-web. First, we need to review briefly what a URL is:

URL stands for Uniform Resource Locator, and is the standard way to address files located on the Internet. A URL is divided into five parts, not all of which need to be present for every URL:
The protocol is the "language" that needs to be used to retrieve data. It usually is "http", but other valid protols that every web browsers understands is "ftp", "telnet", or "mailto".
The is the name of a machine on the Internet. A machine name is usually divided into several pieces, separated by dots. For example, "" and "" are two machine names of two different machines that are part of the Seton Hall network.
The directory is one or more directories where the file that you want to access is located. Directories are separated by forward-slashes. For example, /Teaching/CSAS4083/Lectures are three directory names where these lectures can be found.
The file name is the "first name" of the file you want to access. For example, lecture1.html is a file name which has "lecture1" as its "first name"
The extension is comprised of the part of the file name after the last period. On the web, this extension is used to indicate the type of the file. For example, files with the ".html" extension are web documents, files with the ".gif" extension are images, files with the extension ".wav" or ".au" are sound files, and so on.

Some valid URL's are, for example:

the protocol is "http", the machine to access is "", and the directory, file name, and extension are missing. The Seton Hall web browser will, in this case, automatically provide a default directory and file name.

the protocol is "http", the machine to access is "", the directory to switch to is "projects/reals", the file name is "index.html" which is a web document because of the ".html" extension


the protocol is "telnet", the machine to access is "". The rest is missing. If your web browser is setup correctly, this will bring up the "Telnet" program, connect you to Sciris, and wait for your username and password

Within a Java applet, you can direct the web browser to display any page on the web, either in the current window, or in a new window. There are two parts to this:

  1. create the appropriate URL to load
  2. tell the browser to load that URL

Again, from the introduction we know that there are two methods that deal with URL's:

public URL getCodeBase();
returns the URL of the location for the currently loaded applet
public URL getDocumentBase();
returns the URL of the location for the HTML document that contains the applet

The CodeBase and the Document base can be different. For our examples so far, however, they have always been the same. We will see later how, and why, they can can be different. Finally, the method(s) that tell your web browser to diplay a certain web page are located in the AppletContext class and are called "showDocument":

So, here is a very simple example of an applet that contains one button. If you click on that button, the index of lectures will be displayed in the current window - try it out, then use the "back" button of your browser to return to this page. First, here is the source code:
import java.applet.*;  // NOTICE that we now display java.applet.* instead of java.applet.Applet
import java.awt.*;

public class ShowDocumentTest extends Appplet
   private Button show = new Button("Show URL");

   public void init()
      setLayout(new FlowLayout());
   public boolean action(Event e, Object o)
      if ( == show)
         getAppletContext().showDocument(getDocumentBase(), "../index.html");
      return true;

Here the "getDocumentBase()" returns the URL where this page is located, but without the file name. In this case, it is:

Then, the next string says to go - from that location - one level up, and there use the file name "index.html".

To load other documents, not based on the current URL, we first need to learn about creating URL's in Java.

To create a URL,  you need to import the class "" and use the constructors of that class. Without going too much into details, there are two URL constructors. Each one throws an exception which need to be caught in the usual try-catch block. The two constructors are (taken from the Java API):
public URL(String spec) throws MalformedURLException

Creates a URL object from the String representation.

public URL(URL context, String spec) throws MalformedURLException

Creates a URL by parsing the String specification within a specified context: If the context argument is not null and the spec argument is a partial URL specification, then any of the strings missing components are inherited from the context argument.

The specification given by the String argument is parsed to determine if it specifies a protocol. If the String contains an ASCII colon ':' character before the first occurrence of of an ASCII slash character '/', then the characters before the colon comprise the protocol.

If the spec argument does not specify a protocol:
If the context argument isn't null, then the protocol is copied from the context argument.
If the context argument is null, then a MalformedURLException is thrown.
If the spec argument does specify a protocol:
If the context argument is null, or specifies a different protocol than the specification argument, the context argument is ignored.

The first constructor is useful to create an 'absolute' URL, i.e. an address which specifies each part, including the protocol, the macine name, and the directory and file names. The second constructor is useful to create 'relative' URL address, i.e. addresses that work relative to the current location of the web page containing the applet, or the machine where the applet originates. For example (try-catch blocks omitted):
URL url = new URL(""); 
URL url = new URL("");
URL url = new URL(""); 
URL url = new URL("");

are all legal, absolute URL's. Each could be used for the showDocument method. The first one would direct the browser to bring up Seton Hall's homepage, the second one would invoke the mail method of the web browser to allow sending an email message to '', and the third one would tell the browser to open an FTP connection to the machine named '' and login to that machine using the username 'anonymous'. Finally, the last would invoke the telnet program that has been setup with your web browser, connect to machine '' on port 23, and login as JohnDoe. You will have to provide the password as usual for a telnet session.

The second constructor is perhaps most useful in conjuction with the methods that are part of the Applet class:
public URL getCodeBase()
Returns the URL of this applet.
public URL getDocumentBase()
Returns the URL of the document that contains this applet.

Therefore, one could use a URL constructor as follows:
URL url = new URL(getCodeBase(),"whatever.html"); 
URL url = new URL(getDocumentBase(),"whatever.html");

Here is an applet that will bring up various pages in your web browser:
import java.applet.*;
import java.awt.*;

public class URLShow extends Applet
   private Button shu = new Button("SHU Homepage");
   private Button yahoo = new Button("Yahoo Homepage");
   private Button bert = new Button("Wachsmuth's Hompage");

   private URL shuURL = null;
   private URL yahooURL = null;
   private URL bertURL = null;

   private AppletContext browser = null;

   public void init()
      setLayout(new GridLayout(3,1));

         shuURL = new URL("");
         yahooURL = new URL("");
         bertURL = new URL("");
      catch(MalformedURLException e)
         showStatus("invalid URL in applet ...");

      browser = getAppletContext();

   public boolean action(Event e, Object o)
      if ( == shu)
      else if ( == yahoo)
      else if ( == bert)
         browser.showDocument(bertURL, "_new");
      return true;

Note that in the catch phrase of the try-catch block necessary when constructing a URL our applet will display an error message in the status bar of the browser. Also, the last "showDocument" method contains an additional string "_new". That will cause the web page to be displayed in a  new Window, leaving the current window as is - try it below !