The URL Class
Bringing this down to a more concrete level is the Java URL class. The URL class represents a URL address and provides a simple API for accessing web resources, such as documents and applications on servers. It can use an extensible set of protocol and content handlers to perform the necessary communication and in theory even data conversion. With the URL class, an application can open a connection to a server on the network and retrieve content with just a few lines of code. As new types of servers and new formats for content evolve, additional URL handlers can be supplied to retrieve and interpret the data without modifying your applications.
A URL is represented by an instance of the java.net.URL class. A URL object manages all the component information
within a URL string and provides methods for retrieving the object it
identifies. We can construct a URL
object from a URL string or from its component parts:
try{URLaDoc=newURL("http://foo.bar.com/documents/homepage.html");URLsameDoc=newURL("http","foo.bar.com","documents/homepage.html");}catch(MalformedURLExceptione){...}
These two URL objects point to
the same network resource, the homepage.html document
on the server foo.bar.com. Whether the resource
actually exists and is available isn’t known until we try to access it.
When initially constructed, the URL object contains only data about the object’s location and how to access it. No connection to the server has been made. We can examine the ...