Solve the problem of Chinese garbled characters in Get request parameters
Tips before viewing:
The IDEA version used in this article is ultimate 2019.1, the JDK version is 1.8.0_141, and the Tomcat version is 9.0.12.
In the project a few days ago, when using get to send a url request to send parameters in Chinese, garbled characters appeared. After searching for the reason on the Internet, the following solutions were provided.
1.encodeURI
1.1 Front end
The front-end jsp page part of the code is as follows
<script type="text/javascript">
function doSubmit(){
var url = "${pageContext.request.contextPath}/test/testEncodeURI"
+ "?userName=" + encodeURI(encodeURI("张三"));
window.location.href = url;
}
</script>
Note
1.2 Backstage
The background test Controller is as follows
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
/**
* 测试Controller
* @author jjy
* @date 2020-07-21
*/
@Controller
@RequestMapping("/test")
public class TestController {
/**
* 测试encodeURI
* @param request
* @return
*/
@RequestMapping("testEncodeURI")
public String testEncodeURI(HttpServletRequest request){
String userName = request.getParameter("userName");
System.out.println("Before encodeURI : " + userName);
try {
userName = URLDecoder.decode(userName, "UTF-8");
} catch (Exception e){
e.printStackTrace();
}
System.out.println("After encodeURI : " + userName);
return "index";
}
}
Note
The results are as follows
1.3 Summary
-
The encoding and decoding process is as follows:
UTF-8 encoding->UTF-8 (iso-8859-1) encoding->iso-8859-1 decoding->UTF-8 decoding, the encoding and decoding process is symmetrical, so it will not appear Garbled. -
The front end encodeURI twice: The
encodeURI function uses utf-8 for encoding, and when the server is decoding, it is not decoded by uft-8 by default, so garbled characters will appear.
EncodeURI twice, the URL obtained in the first encoding is UTF-8, and the URL obtained in the second encoding is still in UTF-8, but in effect, it is equivalent to performing UTF-8 encoding first (at this time All have been converted to ASCII characters), and the iso-8859-1 encoding is performed again, because UTF-8 encoding and ISO-8859-1 encoding have the same effect for English characters. -
Background decoding:
When receiving parameters in the background, first automatically decode the first time through request.getParameter() (it may be gb2312, gbk, utf-8, iso-8859-1 and other character sets, which has no effect on the result) to obtain ascii characters , And then use UTF-8 for the second decoding, usually using java.net.URLDecoder("","UTF-8") method.