Home > Java > JavaBase > body text

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

Release: 2019-12-06 13:56:55
Original
1986 people have browsed it

Solution to garbled characters when url is transmitted in 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. Recommended: java video tutorial

Solution

1. Encode the Chinese characters to be transmitted in the URL:

String message = java.net.URLEncoder.encode("中文字符","utf-8");
Copy after login

2. Get the URL Pass the Chinese page to decode the 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 do we need to convert the extracted character set format 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 the java basic tutorial column.

The above is the detailed content of Solution to garbled characters when url is transmitted in 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