Java Reference
In-Depth Information
• The plus sign “+”/“%20” is converted into a space character “ ”.
• Asequenceoftheform% xy willbetreatedasrepresentingabytewhere xy is
thetwo-digithexadecimalrepresentationofthe8bits.Then,allsubstringscon-
tainingoneormoreofthesebytesequencesconsecutivelywillbereplacedby
the character(s) whose encoding would result in those consecutive bytes. The
encoding scheme used to decode these characters may be specified; when un-
specified, the platform's default encoding is used.
URLDecoder declares the following class method for decoding an encoded string:
String decode(String s, String enc)
This method decodes an application/x-www-form-urlencoded string us-
ing the specified encoding scheme. The supplied encoding is used to determine what
charactersarerepresentedbyanyconsecutivesequencesoftheform% xy . Unsuppor-
tedEncodingException is thrown when enc 's value isn't supported.
Therearetwopossiblewaysinwhichthedecodercoulddealwithillegallyencoded
strings.Itcouldeitherleaveillegalcharactersaloneoritcouldthrow IllegalArgu-
mentException . Which approach the decoder takes is left to the implementation.
Note The World Wide Web Consortium Recommendation ( ht-
tp://www.w3.org/TR/html40/appendix/notes.html#non-ascii-
chars ) ,whichissimilartoanRFC,statesthatUTF-8shouldbeusedastheencoding
schemefor encode() and decode() .Notdoingsomayintroduceincompatibilities.
I've created an application that demonstrates URLEncoder and URLDecoder in
the context of the previous "the string ü@foo-bar" and
"the+string+%c3%bc%40foo-bar" example. Listing 9-9 presents the applica-
tion's source code.
Listing 9-9. Encoding and decoding an encoded string
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
class EncDec
{
public static void main(String[] args) throws Unsuppor-
Search WWH ::




Custom Search