home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Most of this applet is familiar to you by now. (If you’re new to the Abstract Windowing Toolkit, take a look at Chapter 38, “User Input and Interactivity with Java.”) This applet has three buttons, each with a Web destination name. When you click a button, it fires the linkTo() method. This code is where the new Web-specific information appears.

First, we make a new URL object from the String. If the URL constructor cannot make sense out of theURLString, it throws MalformedURLException. Given a valid URL, linkTo() now transfers control to the browser’s AppletContext and asks it to show the specified URL in the browser’s topmost window.


If your applet is buried down inside an HTML frame, the showDocument(String) page will also appear inside that frame. In this case, all three URLs should appear on a page by themselves, not in their own frames. You can get that behavior by adding “_top”, as shown in line 40.

This code has only three executable lines per button and three executable lines in linkTo(). Of course, it takes advantage of the fact that it’s running inside a Web browser. The browser supplies the AppletContext, and it’s the browser that actually fetches the document specified by the URL.

Calling Back the Server with openStream()

From within either an applet or an application, you can open a connection to a URL and read back the contents. The basic mechanism is a URL method called openStream():

 try {
   DataInputStream theData =
     new BufferedReader(new
       InputStreamReader(theURL.openStream()));
   String aLine;
   while ((aLine = theData.readLine()) != null) {
     System.out.println(aLine);
   }
 } catch (IOException e) {
   System.err.println(“IOException: ” + e.getMessage());
 } finally {
   theData.close();
 }

Line 4 uses openStream() to retrieve the InputStream. URL.openStream() is really shorthand for

theURL.openConnection().getInputStream();

but most programmers will appreciate the shorter version.

Also on the first line, we wrap the InputStream first in an InputStreamReader (in order to bridge between the byte-oriented world of streams and the char-oriented world of readers). Then we wrap the InputStreamReader in a BufferedReader for efficiency—we’d prefer to read the stream a buffer at a time, rather than a byte at a time!

Finally, we call theData.readLine() successively, bringing in lines of data from the server. For this simple code snippet, we just send the data back out to standard out.

If anything goes wrong, we’ll throw an IOException somewhere in there and catch it on the way out. Whether we throw an exception or not, we’ll always execute the finally clause, closing the stream.


CAUTION:  

If you compile the URL.openStream() code into an applet and attempt to fetch data from some Web server, it probably won’t work. Most browsers include a SecurityManager that reports a SecurityException as soon as you attempt to contact a host other than the one you were downloaded from. To make this code work in an applet, either restrict yourself to connecting only to the applet’s home server or negotiate higher rights with the browser’s SecurityManager.


  To learn more about applet security, see “Executable Content and Security,” p. 1142.

How the URL Class Works

The URL class contains constructors and methods for managing a URL—an object or service on the Internet. The TCP protocol requires two pieces of information: the IP address and the port number. So how is it possible that when you type

http://www.yahoo.com/

you get Yahoo!’s home page?

First, Yahoo! has registered its name, enabling yahoo.com to be assigned an IP address (say 205.216.146.71). This address is resolved by using your system’s domain name resolution service.

Now what about the port number? If not specified, the server’s default port is used—for the Web, that’s port 80.

The URL class supports four constructor variations:

public URL(String spec) throws MalformedURLException;
public URL(String protocol, String host, int port, String file) throws
⇒MalformedURLException;
public URL(String protocol, String host, String file) throws
⇒MalformedURLException;
public URL(URL context, String spec) throws MalformedURLException;

You can thus specify each piece of the URL, as in URL(“http”,“www.yahoo.com”,80,“index.html”), or enable the defaults to take over, as in URL (“http://www.yahoo.com/”), letting Java figure out all the pieces.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.