The GET method submits Chinese Encoding Problems and three solutions, get Chinese Encoding
The GET method is very commonly used in the WEB. Sometimes we have to submit a request in the GET method, but TOMCAT and other containers are always tossing about the encoding problem of the GET method.
Let's talk about the process first:
If our content is sent in GET mode, the content will be followed by the URL. The request sent from the browser is encoded by the URI of the browser and sent to the server, if it is struts2, it will undergo URI decoding through the interceptor and use "iso8859-1" for encoding because the transfer process is binary.
There are three methods to solve the Chinese encoding problem.
Hypothesis:
Url: http: // localhost: 8080/test? Name = test
I. processing on the server
The obtained content is "iso8859-1", using
String str = new String (name. getByte ("iso8859-1"), "UTF-8 ");
Get the correct value.
Ps: This method has a disadvantage; in this way the server must use the iso8859-1 encoding by default, if I change the default iso8859-1 to UTF-8 in TOMCAT, then this method will not take effect.
2. Set it in the TOMCAT configuration file
In the TOMCAT directory, conf/server. in xml, search for the "8080" port. If you change the port to search by port, search for it and enter URIEncoding = "UTF-8" in the space in this tag, which is a iso8859-1 by default.
Ps: the disadvantage of this method is obvious. If you change the server, you have to change it. Sometimes it is very common to forget it.
3. manual encoding once
The browser and server are both encoded and decoded before transmission, and the support for Chinese is not very good. Can you convert Chinese into English or digital transmission in advance?
Hypothesis:
1. name = test. In the JS tag, use the encodeURI (param) method to convert "test" to name = % 26% 92 (NLE) the browser then automatically performs URI encoding name = % 2525% 2592 (% URI encoding is % 25 );
In the ognl expression, <s: param name = "name" value = "% {@ java.net. URLEncoder. encode (name, 'utf-8')} "> URI encoding is performed on the value, and java.net is enabled in the struts2 configuration file. URLEncoder. encode ().
Struts. xml: name = "struts. ognl. allowStaticMehtodAccess" value = "true"
2. At this time, the GET method of the link is submitted.
3. name = % 26% 92 is obtained from the server. We can use URIDecoder. decode (name, "UTF-8"); to decode the server and get a "test"
PS: Is this method good and does not rely on TOMCAT?