i need one complete answer with output

Lab 6: Getting Web Pages


  • public final InputStream openStream() throws IOException

The openStream() method connects to the resource referenced by the URL, performs any necessary handshaking between the client and the server, and then returns an InputStream from which data can be read.

The data you get from this InputStream is the raw (i.e., uninterpreted) contents of the file the URL references: ASCII for an ASCII text file, raw HTML if you are reading an HTML file, etc. It doesn’t include any of the HTTP headers. For example:

try {

URL u = new URL(“ http://www.hamsterdance.com “);

InputStream in = u.openStream();

int c;

while ((c = in.read()) != -1) System.out.write( c);

}

catch (IOException e) {

System.err.println(e);

}


Your Lab 6 assignment is to write a simple application program that reads a URL from the command line, opens an InputStream from that URL, does the necessary filter chaining using the default encoding, and then use an appropriate read method to read successive characters from the file, each of which is printed out on System.out. That is, your program prints the raw data located at the URL; if the URL references an HTML file, the program’s output is raw HTML.


You may test your program on www.gannon.edu or www.hamsterdance.com .

Now, modify your program to display the URL or the web page. You may want to look into using a javax.swing.JEditorPane class. This class provides a complete HTML 3.2 render that can handle frames, forms, hyperlinks, and parts of CSS Level 1.