Java encoding and decoding, Java encoding and decoding

Source: Internet
Author: User

Java encoding and decoding, Java encoding and decoding

Today, a problem occurs when a user fails to log on. However, logon by other users is normal. After debugging, it is found that the password of a user who fails to log on has two special characters: * and :*,#.

When submitting a form with special symbols, the encoding is not the same. So what is the encoding ??

 

1. What is an application/x-www-form-urlencoded string?

It is an encoding type.

When a URL contains strings other than Western European characters, the system converts these characters to application/x-www-form-urlencoded strings.

This is also true when a form is submitted. When a string contains non-Western European characters, the system also converts these characters to the application/x-www-form-urlencoded string.

Package com. app; import java. io. unsupportedEncodingException; import java.net. URLDecoder; import java.net. URLEncoder; public class AA {public static void main (String [] args) {/*** convert the application/x-www-form-urlencoded String to a 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 a normal string to an application/x-www-form-urlencoded string * it must be emphasized that the encoding method must be correct, for example, baidu is gb2312, while google is UTF-8 */String urlStr = ""; try {urlStr = URLEncoder. encode ("document", "Utf-8");} catch (UnsupportedEncodingException e) {e. printStackTrace ();} System. out. println (urlStr );}}

 

The running result is:

Document
% E6 % 96% 87% E6 % A1 % A3

 

2. URLEncoder and URLDecoder

In java1.3 and earlier versions, a new encoded string is returned. encode () uses the default encoding format of the platform.

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 format, such as "UTF-8", "gb2312 ".

Encoding: public static String encode (String s, String encoding) throws UnsupportedEncodingException

Decoding: public static String decode (String s, String encoding) throws UnsupportedEncodingException

Note: If you have no idea which encoding method to use, choose UTF-8. It is more likely to get the correct result than any other encoding format.

 

3. Why encoding?

Answer: handle differences between different operating systems

One of the challenges web designers face is how to handle the differences between different operating systems. These differences cause URL problems: for example, some operating systems allow file names to contain space characters, and some do not.

Most operating systems do not think that the file name contains 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, have special meanings in URLs or other operating systems and express similar problems. To solve these problems

The characters used in the URL must be elements in the fixed character set of an ASCII character set, as follows:

1. capital letter A-Z
2. lowercase letters a-z
3. numbers 0-9
4. Point character -_.! ~ * '(And ,)

If the data submitted to the server contains /&? @ #; $ + = %. These characters and all other characters should be encoded.

The encoding process is very simple. Any character, as long as it is not an ASCII number, letter, or point character, will be converted into bytes. Each byte is written in this form: A "%" is followed by two hexadecimal values.

Space is 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 (+) is encoded as % 2B.

When/# = & and? When used as part of the name, rather than as a separator between the URL, they should be encoded.

 

4. How to encode it?

The class URL does not automatically perform encoding or decoding. Fortunately, java provides a class URLEncoder that encodes string into this form.

Java adds a class URLDecoder which can decode strings in this form.

 

5. When a form in a webpage is submitted using the POST method, the data content type is application/x-www-form-urlencoded. This type will:

1. character "a"-"z", "A"-"Z", "0"-"9 ",". ","-"," * ", and" _ "are not encoded;
2. Convert the space to the plus sign (+ );
3. convert non-text content to "% xy". xy is a two-digit hexadecimal value;
4. Place & symbol between each name = value Pair

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.