Java Reference
In-Depth Information
public String getRef()
The getRef() method returns the fragment identifier part of the URL. If the URL doesn't
have a fragment identifier, the method returns null . In the following code, getRef()
returns the string xtocid1902914 :
URL u = new URL (
"http://www.ibiblio.org/javafaq/javafaq.html#xtocid1902914" );
System . out . println ( "The fragment ID of " + u + " is " + u . getRef ());
public String getQuery()
The getQuery() method returns the query string of the URL. If the URL doesn't have
a query string, the method returns null . In the following code, getQuery() returns the
string category=Piano :
URL u = new URL (
"http://www.ibiblio.org/nywc/compositions.phtml?category=Piano" );
System . out . println ( "The query string of " + u + " is " + u . getQuery ());
public String getUserInfo()
Some URLs include usernames and occasionally even password information. This in‐
formation comes after the scheme and before the host; an @ symbol delimits it. For
instance, in the URL http://elharo@java.oreilly.com/ , the user info is elharo . Some URLs
also include passwords in the user info. For instance, in the URL ftp://
mp3:secret@ftp.example.com/c%3a/stuff/mp3/ , the user info is mp3:secret . However,
most of the time, including a password in a URL is a security risk. If the URL doesn't
have any user info, getUserInfo() returns null .
Mailto URLs may not behave like you expect. In a URL like mailto:elharo@ibiblio.org ,
“elharo@ibiblio.org” is the path, not the user info and the host. That's because the URL
specifies the remote recipient of the message rather than the username and host that's
sending the message.
public String getAuthority()
Between the scheme and the path of a URL, you'll find the authority. This part of the
URI indicates the authority that resolves the resource. In the most general case, the
authority includes the user info, the host, and the port. For example, in the URL
ftp://mp3:mp3@138.247.121.61:21000/c%3a/, the authority is
mp3:mp3@138.247.121.61:21000, the user info is mp3:mp3 , the host is 138.247.121.61 ,
and the port is 21000 . However, not all URLs have all parts. For instance, in the URL
http://conferences.oreilly.com/java/speakers/ , the authority is simply the hostname con‐
ferences.oreilly.com . The getAuthority() method returns the authority as it exists in
the URL, with or without the user info and port.
Example 5-4 uses these methods to split URLs entered on the command line into their
component parts.
Search WWH ::




Custom Search