If you thinks a web is that weelittle fly-catching thing in the corner of your room, you've probably been living in the Himalayas. Today, each and every little Tyke (and his dog), are on the World Wide web. And if you are reading this, then more certainly than your dog's name (Bruno?), you are conversant with the powers of this blown-up scaffold of computer networks. The bad news is that if you feel writing programs in the field of networking, say for something like a live world-wide chat, is a nightmare, then you are probably feeling right. The good news, however, is that before a couple of shakes of Bruno's tail, you will be writing your first Networking program! The program that we'll build on right now, is to yank in a document lurking somewhere on the web (doesn't everything just lurk somewhere out there?) into our computer. And if its kinda hard-hitting, try not to weasel out midway; it will all begin to make sense soon.
Albeit an elaborate explanation is absconding now, we expect you to try this out. Meanwhile, we will do some more slogging :)
Before we swoop down on the actual clutter of Resource Locators, servers, streams etc., let us remind ourselves of a basic list and a button.
Program 1
import java.awt.*; import java.applet.*; public class zzz extends Applet { public void init() { setLayout(new BorderLayout()); List l; l = new List(3000, false); add("Center", l); add("North", new Button("Start")); } }
Not as bed-wettingly scary as you thought it would be, was it? Only the vocabulary of the programming luminaries seems to be bloated with jargon. If you've been using the web, the kinda highfalutin word URL (Uniform Resource Locator) is perhaps at least halfway memorable by now (we wonder why it is called a URL, but we understand one can live out a full and relatively cheerful life without ever needing to know) Anyway, you will get these errors as of now...import java.awt.*; import java.applet.*; public class zzz extends Applet { URL u; public void init() { setLayout(new BorderLayout()); List l; l = new List(3000, false); add("Center", l); add("North",new Button("Start")); } public boolean action ( Event e, Object o) { if ( "Start".equals(o) ) { try { u=new URL("http://www.neca.com/~vmis/java.html"); showStatus(" URL is initialised "); } catch(MalformedURLException x) { showStatus("error in URL"); } } return true; } }
class URL not found in type declaration
class URL not found in new
class MalformedURLException not found in type declaration
class MalformedURLException not found in catch
Therefore, we use the inevitable import , which, incidentally, is mandatory if you want to get past the compilation stage!
With that wisdom up our sleeve, let us give it another try. We first have to define a URL connection . Right after the line that saysimport java.net.*;
And later, in the action() , we write the following...URLConnection c;
But again, there are three errors lobbed at us which very rhetorically go as...try { c = u.openConnection(); showStatus("Open Connection successful"); } catch(IOException x) { showStatus ("Connection failed"); }
Exception :java.io.IOException must be caught , or it must be declared in
the throws clause of this method
Class IOException not found in type declaration
Class IOException not found in catch
And that brings us to yet another import statement. This one is for the IO (and for those who came in late or didn't come at all, that stands for "Input Output").
As yet, whatever we are doing with our code is far from spectacular. While we are taking care of the URL and the URL connection, we conveniently forget about fetching in the file. Towards that end, we have a class called InputStream . Hence, we include the following line...import java.io.*;
Besides, our action() will now look like this...InputStream n;
public boolean action ( Event e, Object o) { if ( "Start".equals(o) ) { try { u=new URL("http://www.neca.com/~vmis/java.html"); showStatus(" URL is initialised "); } catch(Exception x) { showStatus("URL not initialised"); return true; } try { c = u.openConnection(); showStatus("Open Connection successful"); } catch(Exception x) { showStatus("Error in Open Connection"); return true; } try { n = c.getInputStream(); showStatus("getInputStream successful"); } catch(Exception x) { showStatus("error in getInputStream"); return true; } } return false; }
Next we have the DataInputStream class which will likewise be inserted in the following way...
The action() will now become...DataInputStream d;
Note that this time we have a single catch for all the exceptions instead of having a barrage of them (though having them seperately shown is good if you want to see the exceptions explicitly). Let us also have a String and place it in the action() .public boolean action ( Event e, Object o) { if ( "Start".equals(o) ) { try { u=new URL("http://www.neca.com/~vmis/java.html"); showStatus(" URL is initialised "); c = u.openConnection(); showStatus("Open Connection successful"); n = c.getInputStream(); showStatus("Input Stream successful"); d=new DataInputStream(n); showStatus(" Data Input Stream successful"); } catch(Exception x) { showStatus("error in URL/Connection"); } return true; } return false; }
action()String s;
Let us add this to the list.try { s=d.readLine(); showStatus ("String "+s); } catch(Exception x) { showStatus("String not read"); return true; }
It must be at least halfway clear that the string is being used to display inside the listwhatever has come in as a file. But the problem here is that the file is not being properly read. We'll have a loop for the reading, so that the complete document has been read.try { l.addItem(s); d.close (); n.close(); } catch(Exception x) { showStatus("Add Item not successful"); return true; }
public class zzz extends Applet { URL u; URLConnection c; InputStream n; DataInputStream d; String s; List l; public void init() { setLayout(new BorderLayout()); l = new List(3000,false); add("Center",l); add("North",new Button("Start")); } public boolean action ( Event e, Object o) { if ( "Start".equals(o) ) { try { u=new URL("http://www.neca.com/~vmis/java.html"); showStatus(" URL is initialised "); c = u.openConnection(); showStatus("Open Connection successful"); } catch(Exception x) { showStatus("error in URL/Connection"); } try { n = c.getInputStream(); showStatus("Input Stream Open successful"); } catch(Exception x) { showStatus("error in get Input Stream"); return true; } try { d=new DataInputStream(n); showStatus(" Data Input Stream Open successful"); } catch(Exception x) { showStatus ("error in Data Input Stream"); return true; } try { while ((s=d.readLine()) != null ) l.addItem(s); n.close(); } catch(Exception x) { showStatus("Add Item not successful"); return true; } } return false; } }
Unless its time to take Bruno for a walk, get to the next topic "Miscellaneous Code Dump" or just reach your canine gnarls :) i.e., your comments, feedback, suggestions et al to us before he begins to claw at your door.