24-12-2012, 05:52 PM
URLs and URIs
URLs and URIs.ppt (Size: 657 KB / Downloads: 34)
URL Class
The fields of java.net.URL are visible only to other members of the java.net package.
URL class has a single method for setting the fields of a URL.
After a URL object has been constructed, its fields do not change.
Creating New URLs
Unlike the InetAddress objects, we can construct instances of java.net.URL.
There are six constructors for creating URLs.
All these constructors throw a MalformedURLException if you try to create a URL for an unsupported protocol.
Exactly which protocols are supported is implementation dependent.
URLEncoder and URLDecoder
One of the problems that the designers of the Web faced was differences between local operating systems.
These differences can cause problems with URLs: for example, some operating systems allow spaces in filenames.
To solve these problems, characters used in URLs must come from a fixed subset of ASCII.
If ASCII characters occur as part of a filename, then they and all other characters should be encoded.
Any characters that are not ASCII numerals, letters, or the punctuation marks specified earlier are represented by a percent sign followed by two hexadecimal digits giving the value for that character.
URLEncoder
The java.net.URLEncoder class contains a single static method called encode( ) that encodes a String according to these rules:
public static String encode(String s)
It changes any nonalphanumeric characters except the space, underscore, hyphen, period, and asterisk characters into % sequences.
Constructing a URI
URIs are built from strings.
Unlike the URL class, the URI class does not depend on an underlying protocol handler.
As long as the URI is syntactically correct, Java does not need to understand its protocol in order to create a representative URI object.