I believe many friends have encountered the problem of garbled characters when transmitting Chinese to URL in Java. Recently, I encountered a problem, which is to bind a piece of Chinese information to the URL in Action. Later, when ActionForward goes to another page, garbled characters will appear when reqeust.getParameter is used to retrieve it.
Solution:
1. Encode the Chinese characters to be passed in the URL:
String message = java.net.URLEncoder.encode("中文字符","utf-8");
2. Decode the characters on the page where the URL passes Chinese characters:
String msg = request.getParameter("message"); String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8");
Note:
1. The str obtained here is the "Chinese character" passed in previously.
2. Why should the extracted character set format be converted into UTF-8 format? This is because ISO-8859-1 is the standard character set used for network transmission in Java, request.getParameter("message"); What you get is still the ISO-8859-1 character set, so you need to convert it.
For more java knowledge, please pay attention to java basic tutorial.
The above is the detailed content of Solution to garbled characters when url is transmitted to Chinese in Java. For more information, please follow other related articles on the PHP Chinese website!