A Solution to the Problem of Chinese garbled code in data transmission between JAVA POST and GET

  • 2020-06-03 08:05:05
  • OfStack

Did N java development has never written a website, recently got busy with an entrepreneur publicity website.
Like everyone else, We have encountered the Chinese character set confusion problem. In order to avoid some detours, we share a simple and practical solution.
1. POST data transmission
Receive page
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
request.setCharacterEncoding("UTF-8"); 
String zh_value=reqeust.getParameter("zh_value"); 
%> 

There are two important points
 
pageEncoding="UTF-8" // Declares the character set for the current page  

 
request.setCharacterEncoding("UTF-8"); // The statement request The character set  

2. GET data transmission
Receive page
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String zh_value=new String(request.getParameter("zh_value").getBytes("ISO-8859-1"),"UTF-8") 
%> 

There are two important points
 
pageEncoding="UTF-8" // Declares the character set for the current page  

 
new String(request.getParameter("zh_value").getBytes("ISO-8859-1"),"UTF-8") // Will get the string according to the first ISO-8859-1 Decoded into byte Array based on UTF-8 will byte Array generated string  

If you can't solve the problem by using the above methods, please be sure to leave a message to me. I can add more methods for your reference.

Related articles: