Home  >  Article  >  Java  >  Solution to garbled request in java

Solution to garbled request in java

尚
Original
2019-12-30 09:43:022670browse

Solution to garbled request in java

Solution to Chinese garbled POST:

POST passes parameters through the request body. Set the encoding method of the response character stream to UTF-8.

request.setCharacterEncoding("UTF-8"); This sentence solves the problem of Chinese garbled characters submitted in POST mode. This method sets the character encoding in the request body, so the server will parse it according to UTF- 8 is decoded. But this method has no effect on the get method.

(Recommended: java video tutorial)

GET Chinese garbled code solution

GET method passes parameters through the url.

// 下面的方式可以解决post或者get方式的中文乱码问题
// 这里接收的name是iso8859-1的字符编码
String name = request.getParameter("username");
// 将name字符串按照原来字符编码打散
byte[] bytes = name.getBytes("ISO8859-1");
// 将bytes字节数据按照指定字符编码字符编码进行组装,组装为String
name = new String(bytes, "UTF-8");
//真正的写法:name = new String(name.getBytes("ISO8859-1"), "UTF-8");

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

The above is the detailed content of Solution to garbled request in java. 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