URL
class provides two additional constructors for creating a URL
object. These constructors are useful when you are working with URLs, such as HTTP URLs, that have host name, filename, port number, and reference components in the resource name portion of the URL. These two constructors are useful when you do not have a String containing the complete URL specification, but you do know various components of the URL. For example, suppose you design a network browsing panel similar to a file browsing panel that allows users to choose the protocol, host name, port number, and filename. You can construct a
URL
from the panel's components. The first constructor creates a URL
object from a protocol, host name, and filename. The following code snippet creates a URL
to the Gamelan.net.html
file at the Gamelan site: This is equivalent tonew URL("http", "www.gamelan.com", "/pages/Gamelan.net.html");
The first argument is the protocol, the second is the host name, and the last is the pathname of the file. Note that the filename contains a forward slash at the beginning. This indicates that the filename is specified from the root of the host. The finalnew URL("http://www.gamelan.com/pages/Gamelan.net.html");
URL
constructor adds the port number to the list of arguments used in the previous constructor: This creates aURL gamelan = new URL("http", "www.gamelan.com", 80, "pages/Gamelan.network.html");
URL
object for the following URL: If you construct ahttp://www.gamelan.com:80/pages/Gamelan.network.html
URL
object using one of these constructors, you can get a String
containing the complete URL address by using the URL
object's toString
method or the equivalent toExternalForm
method.
No comments:
Post a Comment