Parsing URLs

The java.net.URL class has five methods to split a URL into its component parts. These are:

 public String getProtocol()
 public String getHost()
 public int    getPort() 
 public String getFile()
 public String getRef()

For example,

 try {
    URL u = new URL("http://www.poly.edu/schedule/fall97/bgrad.html#cs");
    System.out.println("The protocol is " + u.getProtocol());
    System.out.println("The host is " + u.getHost());
    System.out.println("The port is " + u.getPort());
    System.out.println("The file is " + u.getFile());
    System.out.println("The anchor is " + u.getRef());
  }
  catch (MalformedURLException ex) {
  
  }

If a port is not explicitly specified in the URL, it's set to -1. This does not mean that the connection is attempted on port -1 (which doesn't exist) but rather that the default port is to be used.

If the ref doesn't exist, it's just null, so watch out for NullPointerExceptions. Better yet, test to see that it's non-null before using it.

Finally if the file is left off completely, e.g. http://java.sun.com, then it's set to "/".


Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified November 18, 1998