Java Reference
In-Depth Information
jeff@tutortutor.ca
is a server-based authority component, in which
jeff
de-
notes the user information and
tutortutor.ca
denotes the host—there's no port.
path
identifies the resource's location according to the authority component (when
present)orthescheme(whentheauthoritycomponentisabsent).Apathdividesintoa
sequenceof
path segments
(portionsofthepath),inwhichforwardslashcharactersare
usedtoseparatethesegments.Thepathisabsolutewhenthefirstpathsegmentbegins
with a forward slash character; otherwise, the path is relative. For example,
/a/b/c
constitutesapathwiththreepathsegments—
a
,
b
,and
c
.Furthermore,thepathisabso-
lute because a forward slash character prefixes the first path segment (
a
).
query
identifies data to be passed to the resource. The resource uses the data to
obtain or produce other data that it passes back to the caller. For example, in
ht-
/software/Aquarium
representsaquery.Accordingtothatquery,
/software/
Aquarium
isdatatobepassedtoaresource(
makepage.cgi
),andthisdatahappens
tobetheabsolutepathtoadirectorywhosesame-namedfileismergedwithboilerplate
HTML by a Perl script to generate a resulting web page.
Thefinalcomponentis
fragment
.AlthoughitappearstobepartofaURI,it'snot.
WhenaURIisusedinaretrievalaction,theprimaryresourcethatperformsthataction
usesthefragmenttoretrievethesubordinateresource.Forexample,
makepage.cgi
is the primary resource and
/software/Aquarium
is the subordinate resource.
The previous discussion reveals that a complete URI consists of scheme, authority,
path, query, and fragment components; or it consists of scheme, user-info, host, port,
path,query,andfragmentcomponents.Toconstructa
URI
instanceintheformercase,
call the
URI(String scheme, String authority, String path,
String query, String fragment)
constructor. In the latter case, call
URI(String scheme, String userInfo, String host, int port,
String path, String query, String fragment)
.
Additional constructors are available for creating
URI
instances. For example,
URI(String uri)
createsa
URI
byparsing
uri
.Regardlessofwhichconstructor
you call, it throws
java.net.URISyntaxException
when the resulting URI
string has invalid syntax.
Tip
The
java.io.File
class declares a
URI toURI()
method that you can
calltoconverta
File
object'sabstractpathnametoa
URI
object.TheinternalURI's
scheme is set to
file
.