Java Reference
In-Depth Information
Figure 17.10
The URLDemo class displays each part of a URL.
The URL class contains many methods for accessing the various parts of the
URL being represented. Some of the methods in the URL class include the
following:
public String getPath(). Returns the path of the URL.
public String getQuery(). Returns the query part of the URL.
public String getAuthority(). Returns the authority of the URL.
public int getPort(). Returns the port of the URL.
public int getDefaultPort().
Returns the default port for the protocol of
the URL.
public String getProtocol(). Returns the protocol of the URL.
public String getHost(). Returns the host of the URL.
public String getFile(). Returns the filename of the URL.
public String getRef(). Returns the reference part of the URL.
public URLConnection openConnection() throws IOException. Opens a
connection to the URL, allowing a client to communicate with the resource.
The following URLDemo program demonstrates what these methods do
and also demonstrates the various parts of a URL. A URL is entered on the
command line, and the URLDemo program outputs each part of the given
URL. Study the program and try to determine what the output is when the
command-line argument is:
http://www.javalicense.com/courseware/index.html?title=btw#mid
The output of the URLDemo program with this URL is shown in Figure 17.10.
import java.net.*;
import java.io.*;
public class URLDemo
{
public static void main(String [] args)
{
Search WWH ::




Custom Search