FileDocCategorySizeDatePackage
URLRequestor.javaAPI DocExample2367Thu Apr 03 15:16:04 BST 1997None

URLRequestor

public class URLRequestor extends Frame

Fields Summary
TextField
url
TextArea
textDisplay
Constructors Summary
public URLRequestor()

  
    super("URL Requestor");
    resize(500,300);
    move(50,50);
    init();

  
Methods Summary
public booleanaction(java.awt.Event e, java.lang.Object o)

  
    if (e.target instanceof Button) {
      fetchURL(url.getText());
      return true;
    }
    else if (e.target == url) {
      fetchURL(url.getText());
      return true;
    }
    else {
      return false;
    }
  
  
public voidfetchURL(java.lang.String s)

 
   try {
     URL u = new URL(s);
     try {
       Object o = u.getContent();
       if (o instanceof InputStream)  {
         showText((InputStream) o);
       }
       else {
         showText(o.toString());
       }
     }
     catch (IOException e) {
       showText("Could not connect to " + u.getHost());
     }
     catch (NullPointerException e) {
       showText("There was a problem with the content");
     }
   }
   catch (MalformedURLException e) {
     showText(url.getText() + " is not a valid URL");
   }
 
 
public voidinit()

  
    textDisplay = new TextArea();
    add("Center", textDisplay);
    // We don't want the buttons and fields in the north and south
    // to fill their respertive sections so we'll add Panels there
    // and use FlowLayout's in the Panels
    Panel SouthPanel = new Panel();
    Panel NorthPanel = new Panel();
    NorthPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    SouthPanel.add(new Button("Get URL"));
    NorthPanel.add("North", new Label("URL: "));
    url = new TextField(40);
    NorthPanel.add("North", url);
    add("South", SouthPanel);
    add("North", NorthPanel);
      
  
public static voidmain(java.lang.String[] args)

  
    URLRequestor a = new URLRequestor();  
    a.show();

  
voidshowText(java.lang.String s)

   textDisplay.setText(s);
 
voidshowText(java.io.InputStream is)

 
   String nextline;
 
   textDisplay.setText("");
   try {
     DataInputStream dis = new DataInputStream(is);
     while((nextline = dis.readLine()) != null) {
       textDisplay.appendText(nextline + "\n");
     }
   }
   catch (IOException e) {
     textDisplay.appendText(e.toString());
   }