Browsers obtain resources from the Web by specifying
the Web addresses, which are referred to officially as Uniform
Resource Locators (URL). For example,
http://www.ibm.com/
The formal description of the URL components goes as follows:
Protocol_ID://Host_IP_address:Port/directory/Filename#Target
where
Protocol_ID
= HTTP, FTP, etc
Host_IP_address
= Host name in either numerical IP or hostname format
Port
= default is 80
Filename
= Name of a hypertext web page or other type of file. Default
index.htm[l]
Target
= optional reference address within a Web page
In Java we can create an instance of the java.net.URL
class. These specify the location of a file on the network. We
have, for example, used URL
objects to obtain the location of image files in applets as shown
here:
getImage( getCodeBase(), filename);
The applet method getCodeBase()
returns a URL
object for the location of the applet file. Here it is assumed
the image file resides in the same directory
The URL
class provides several methods for obtaining URL information (after
a program in the Sun
Java Tutorial):
ParseAddress.java |
import
java.net.*;
import java.io.*;
/** Parse a URL address into its components. **/
public
class ParseAddress
{
public
static void main (String[] args) {
if (arg.length
!=1) {
System.out.println ("Error: missing
the url argument");
System.out.println ("Usage:
java ParseURL <url>");
System.exit (0);
}
try
{
URL
url = new URL (arg[0]);
System.out.println ("Protocol
= " + url.getProtocol ());
System.out.println ("Host
= " + url.getHost ());
System.out.println ("File
name = " + url.getFile ());
System.out.println ("Port
= " + url.getPort ());
System.out.println ("Target
= " + url.getRef ());
}
catch (MalformedURLException
e) {
System.out.println ("Bad
URL = " + arg[0]);
}
} // main
} // class ParseAddress
|
You run the program as follows:
c:\>
java ParseAddress http://www.myschool.edu:80/data/x.html#d
This program outputs:
Protocol
= http
Host = www.myschool.edu
File name = /data/x.html
Port = 80
Target = d
Also, URL's can be constructed from a URL string
URL
kth = new URL ("http//www.kth.se/index.html");
or from separate protocol, host, file strings:
URL
kth = new URL ("http","www.kth.se","/index.html");
When you attempt to create a URL
object, the constructor will validate the components . If they
are in error, it will throw a MalformedURLException.
So you must catch this exception from the URL
constructors.
References & Web
Resources