The URL Class

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 20.  Network Programming


Another useful class in the java.net package is the URL class. A URL (Uniform Resource Locator) is a pointer or handle to a resource on the network. Sometimes this resource is a simple HTML file, and other times it can be a servlet, a Web Service, or a database.

The format of the URL is

 protocol:hostname:port:filename 

In addition, there can also be an optional reference appended to the end.

The URL class can be used to retrieve a resource from the Internet using a program instead of a browser. In fact, you could use this class to write your own browser. Listing 20.12 shows how to use the URL class to download an HTML page.

Listing 20.12 The TextFromURL.java File
 /*   * TextFromURL.java   *   * Created on October 4, 2002, 12:38 PM   */  package ch20;  import java.net.*;  import java.io.*;  /**   *   * @author  Stephen Potts   */  public class TextFromURL  {      int rawData;      /** Creates a new instance of TextFromURL */      public TextFromURL()      {          try          {              URL myURL = new URL("http://java.sun.com");              URLConnection uc = myURL.openConnection();              System.out.println("the host is " + myURL.getHost());              System.out.println("****************************");              System.out.println("Here is the HTML for this page");              InputStream is = myURL.openStream();              while((rawData = is.read()) != -1)              {                  System.out.print((char)rawData);              }              is.close();          }catch (MalformedURLException mue)          {              System.out.println("Exception " + mue);          }catch (Exception e)          {              System.out.println("Exception " + e);          }      }      /**       * @param args the command line arguments       */       public static void main(String[] args)      {          TextFromURL tfu = new TextFromURL();      }  } 

The data will be read in as an integer and converted to a character.

 int rawData; 

The URL class is constructed with the address of the Java Web site at Sun Microsystems.

 URL myURL = new URL("http://java.sun.com"); 

The class that opens the connection is called URLConnection.

 URLConnection uc = myURL.openConnection(); 

The getHost() method returns the name of the host computer for this URL.

 System.out.println("the host is " + myURL.getHost());  System.out.println("****************************");  System.out.println("Here is the HTML for this page"); 

We will use a stream to access this data.

 InputStream is = myURL.openStream(); 

We read the rawData, then cast it to a char before printing it out.

 while((rawData = is.read()) != -1)  {      System.out.print((char)rawData);  } 

If there is a problem with the URL, a MalFormedURLException is thrown.

 }catch (MalformedURLException mue)  {      System.out.println("Exception " + mue);  }catch (Exception e) 

The output from this program is shown here:

 the host is java.sun.com  ****************************  Here is the HTML for this page  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  <HTML>  <HEAD>  <TITLE>The Source for Java(TM) Technology</TITLE>  <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  <META NAME="description" value="Sun Microsystems'  Java Technology Home Page - Visit this site to get the latest Java   technologies, news, and products.">  .  .  .  </tr>  <tr>  <td colspan="7" height="1" bgcolor="#cccccc"><img src="/books/1/89/1/html/2//images/v3_pixel.gif"  width="1" height="1"></td>  </tr>  </table>  </span>          </BODY>          </HTML> 

The dots in the middle of this listing indicate that only the beginning and the end of the listing is shown. The entire listing was about 25 pages long.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net