URL : Java Glossary

URL
URL (Uniform Resource Locator) The old name for URI (Uniform Resource Indicator).RFC 4248 describes URL formats. RFC 3986 describes URI formats. java.net.URL has been replaced by java.net.URI. Strictly speaking, every URL is a URI, but not every URI is a URL. URI ’s are an extension and refinement of the URL. Yet, as classes, neither is a subclass of the other and they have no common base other than Object. java.net. URI is Java’s second attempt at doin a URL class and they started from scratch.

URL s never automatically armour their parameters, where URI s sometimes do.

If a bug report I got is correct, under some conditionsJava version 1.6 URL.openConnection returns null without throwing an IOException.

The term URL can also be an as a verb, as in “Please URL me that picture of the Windows Firewall, meaning “Please email me the URL of that photograph”, in other words email the text: http://mindprod.com/image/jgloss/msfirewall.jpg.

The new URL ( URL , String ) constructor lets you construct a new URL using the original as a context to modify. You do the equivalent thing with URI .resolve( String );

Non-ASCII URL s

What if you wanted to embed the following URL in your HTML (Hypertext Markup Language)?
<a href="http://en.wikipedia.org/wiki/Façade_pattern">
What do you do about that pesky non-ASCII ç character? HTML 4.01 says URLs should only contain ASCII (American Standard Code for Information Interchange) chars, but if non-ASCII chars appear they should be treated as UTF-8. In theory you should be able to encode it with &ccedil;, &#231; or &#xe7;, but in practice, you can’t count on this working. You must use the URL-Encoded %hh escaping mechanism of the equivalent UTF-8 chars like this:
<a href="http://en.wikipedia.org/wiki/Fa%E7ade_pattern">
+ is converted to %2B and space is converted to +.

The URL constructor neither encodes nor decodes. It just leaves your blanks as either blanks or as %20, however you had them.

URLs always use UTF-8 encoding, no matter what the encoding of the enclosing document.

Tips

Component Parts

HTTP (Hypertext Transfer Protocol) : URI Component Parts
http://roedy@www.mindprod.com:80/products/abc.html?type=all&colour=brown#FOXES
Example Part URI.method Name
http://roedy@www.mindprod.com:80/products/abc.html?type=all&colour=brown#FOXES toString url
http getProtocol protocol, scheme
getAuthority authority
roedy getUserInfo Userinfo, email address
www.mindprod.com getHost host
http://www.mindprod.com getProtocol() + ':' getHost() home page
mindprod.com n/a domain
80 getPort port, nearly always 80 for http. http://localhost:8080 accesses a local Tomcat server. http://localhost:4848 accesses a local Glassfish admin port.
/products/abc.html getPath path, URI : URI. This might be a file or it might be a directory. You can’t tell other than by the hint of a dot in the filename. It might be empty or just a single /.
/products n/a just the directory
abc.html n/a just the file
html n/a just the extension
type=all&colour=brown getQuery query, used in CGI (Common Gateway Interface) queries to pass data to the server.
/products/abc.html?type=all&colour=brown getFile path + query if any. It does not get you the file name!
FOXES URL.getRef ref, fragment, reference, target. Not technically part of the URI. Anchor in document to point to.
FOXES URI.getFragment URI equivalent ofURL.getRef. part after #, ref, fragment, reference, target. Not technically part of the URI. Anchor in document to point to.

Learning More

Oracle’s Javadoc on URL class : available:
Oracle’s Javadoc on URI class : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/url.html J:\mindprod\jgloss\url.html
logofeedback Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email If you want your message kept confidential, not considered for posting, please explicitly specify that.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.210]
You are visitor number 20,594.