Introduction: Today encountered a problem, a user logged in when the login failed. However, other user logins are normal, and the user who has been debugged to discover that the login failed has two special characters in the password: *, #.
Special symbols when submitting a form, there is a problem with coding. So what's the code??
1 . What is a application/x-www-form-urlencoded string?
It is a type of encoding.
When a string containing non-Western European characters is included in the URL address, the system converts the characters to a application/x-www-form-urlencoded string.
This is also true when forms are submitted, and the system converts these characters to application/x-www-form-urlencoded strings when they contain strings that are not Western European characters.
PackageCom.app;Importjava.io.UnsupportedEncodingException;ImportJava.net.URLDecoder;ImportJava.net.URLEncoder; Public classAA { Public Static voidMain (string[] args) {/*** Convert application/x-www-form-urlencoded string to normal string*/String KeyWord= ""; Try{KeyWord= Urldecoder.decode ("%e6%96%87%e6%a1%a3", "UTF-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } System.out.println (KeyWord); /*** Convert ordinary string to application/x-www-form-urlencoded string * It must be emphasized that the encoding must be correct, such as Baidu is gb2312, and Google is UTF-8 */String urlstr= "" ; Try{urlstr= Urlencoder.encode ("document", "Utf-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } System.out.println (URLSTR); }}
The operating result is:
Document
%e6%96%87%e6%a1%a3
2,Urlencoder
In java1.3 and earlier versions, a new encoded String,encode () was returned using the platform's default encoding form
Encoding: public static string Encode (string s)
Decoding: public static string decode (string s)
In java1.4 and later, the user is required to specify the encoding form, such as "UTF-8", "gb2312".
Encoding: public static string Encode (string s, String encoding) throws Unsupportedencodingexception
Decode: public static string decode (string s, String encoding) throws Unsupportedencodingexception
Note: If you are uncertain about which encoding to use, then choose UTF-8. It is more likely than any other coding form to get the right results.
3, why do you want to encode?
The answer: dealing with differences between different operating systems
One of the many challenges web designers face is how to deal with the differences between different operating systems. These differences in performance cause URL problems: For example, some operating systems allow filenames to contain whitespace, and some do not.
Most operating systems do not consider the file name to have any special meaning in the symbol "#", but in a URL, the symbol "#" indicates that the file name has ended, followed by a fragment (partial) identifier. Other special characters, non-alphanumeric character sets, which have special meanings on the URL or on another operating system, express similar problems. In order to solve these problems
The character that we use in the URL must be an element in a fixed set of ASCII characters, as follows:
1. Capital Letter A-Z
2. Lowercase letter A-Z
3. Number 0-9
4. Punctuation characters-_. ! ~ * ' (and,)
if the data submitted to the server contains/&? @ #; $ + =%, these characters and all other characters should be encoded.
The encoding process is very simple, as long as any character is not ASCII digits, letters, or punctuation marks mentioned earlier, they are converted to byte form, each byte is written in this form: a "%" followed by a two-bit 16 binary value.
Spaces are a special case, because they are too common. In addition to being encoded as "%20", it can also be encoded as a "+". The plus sign (+) itself is encoded as%2b.
When/# = & and? When used as part of the name, instead of as a delimiter between the URL parts, they should all be encoded.
4, how to encode?
The class URL does not automatically perform encoding or decoding work. Fortunately, Java provides a class Urlencoder to encode string into this form.
Java1.2 adds a class Urldecoder It can decode a string in this form.
5. When a form in a Web page is submitted using the Post method, the type of data content is application/x-www-form-urlencoded, which is:
1. The characters "a"-"Z", "a"-"Z", "0"-"9", ".", "-", "*", and "_" are not encoded;
2. Convert the space to a plus sign (+);
3. Convert non-textual content into "%xy" form, XY is a two-bit 16 binary value;
4. Place the & symbol between each name=value pair
Encoding and decoding in Java