CA597 Tutorial - Using URLs in Applets


Content:
We are going to implement an applet which takes a URL, accesses it, fetches the specified page and displays it.

Network Access

To access a remote URL we need to establish a network connection. Java offers several ways of doing this, e.g. In any case we need to use the java.net package in addition to applet, GUI-elements and event handling.
	import java.applet.*;
	import java.awt.*;
	import java.awt.event.*;
	import java.net.*;
The applet uses similar constructs as the SimpleApplet discussed earlier on: The new bit in this applet is the following:
	System.out.println("Go");
        try
        {
       	          AppletContext context = getAppletContext();
                  URL url = new URL(URLText.getText());
                  context.showDocument(url);
        }
        catch(Exception ex)
        {
                  showStatus("Error "+ex);
        }
The first line is only a test output (you can delete it if you want).

getAppletContext is a method of the Applet class which determines the applet context - information about the environment it is running in. AppletContext is an interface which can be used by an applet to determine concrete information about the context (are there other applets: getApplets() ) or to affect the context. The showDocument(..) method is an example of the latter class.

It requests the browser to show the Web page specified by the URL. URLText is a textfield object. Applying the getText() method gives us the string typed in by the user. If this is actually a valid URL, then a URL object url is created.

Execute the Applet

Here is the applet. Note that the applet is not smart (as most browsers are). You have to give a full URL including the protocol specification, e.g. http: . Most browsers would insert that for you.

Here is the full text of the applet:

//
//      an Applet showing a Web page
//      Claus Pahl
//      Feb 2000
//

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

public class URLApplet extends Applet
                          implements ActionListener
{
        private TextField URLText = null ;
        private Button URLButton = null;

        public URLApplet()
        {
        }

        public void init()
        {
                add(new Label("URL"));
                URLText = new TextField(30);
                add(URLText);
                URLButton = new Button("Go");
                add(URLButton);

                URLButton.addActionListener(this);
        }

        public void actionPerformed(ActionEvent e)
        {
                String actionCommand = e.getActionCommand();
                if (e.getSource() instanceof Button)
                        if (actionCommand.equals("Go"))
                        {
                                System.out.println("Go");
                                try
                                {
                                        AppletContext context = getAppletContext();
                                        URL url = new URL(URLText.getText());
                                        context.showDocument(url);
                                }
                                catch(Exception ex)
                                {
                                        showStatus("Error "+ex);
                                }
                        }
        }
}

How to Extend Your Applet

A warning: the first couple of ideas are about making connections from an applet. Since applets are usually subject to some security restrictions, use the appletviewer in case you experience problems with the browser. What to do??
Continue working on the interface.

© 2000 Claus Pahl