Home > Java > JavaBase > body text

Solution to garbled characters when url is transmitted to Chinese in Java

Release: 2019-11-22 14:00:06
Original
1822 people have browsed it

Solution to garbled characters when url is transmitted to Chinese in Java

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");
Copy after login

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");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template