Home  >  Article  >  Java  >  Java question mark garbled solution

Java question mark garbled solution

尚
Original
2019-11-27 09:15:353839browse

Java question mark garbled solution

In Java-based programming, we often encounter problems with the processing and display of Chinese characters, such as a lot of garbled characters or question marks. (Recommended: java video tutorial)

This is because the default encoding method in JAVA is UNICODE, and the files and DBs commonly used by Chinese people are based on GB2312 or BIG5 and other encodings, so This problem occurs.

1. Output Chinese in the web page.

The encoding used by JAVA in network transmission is "ISO-8859-1", so it needs to be converted when outputting, such as:

String str="中文";
str=new String(str.getBytes("GB2312"),"8859_1");

But if the encoding is used when compiling the program, It is "GB2312", and if you run this program on a Chinese platform, this problem will not occur, so be sure to pay attention.

2. Read Chinese from parameters

This is exactly the opposite of outputting on the web page, such as:

str=new String(str.getBytes("8859_1"),"GB2312");

3. Operation of Chinese problems in DB

A simpler method is: in the "Control Panel", set the "Region" to "English (United States)". If garbled characters still appear, you can also make the following settings:

When getting Chinese: str=new String(str.getBytes("GB2312"));

Enter Chinese into the DB: str =new String(str.getBytes("ISO-8859-1"));

4. Chinese solution in jsp:

In the "Control Panel", change the "Area "Set to "English (United States)".

Add in the JSP page:

If it still cannot be displayed normally, the following conversion must be performed:

For example: name=new String(name.getBytes("ISO-8859-1"),"GBK");

There will be no Chinese problem.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Java question mark garbled solution. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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