How to deal with Chinese parameters

 

 

Why would produce Chinese garbled form

 

Garbled, because the server and client communication coding inconsistencies caused, and therefore the solution is: set up a unified coding between the client and the server, and then it is transmitted and received data in accordance with this coding

 

GET Chinese garbled

// Get the name of the form submission

String name=request.getParameter("name");

name=new String(name.getBytes("ISO-8859-1"),"UTF-8");

 

in

Tomcat7 and the client to encode UTF-8 transmission data to the server, and the server request object using the ISO-8859-1 character code to receive the encoded data inconsistency, the client and server communicate therefore will produce Chinese garbled.

 

 

Solution: After receiving the data, the first acquisition

An array of bytes received request object ISO8859-1 character code to the original data, then the specified string by encoding constructs byte array, to solve distortion problems.

 

Tomcat8 GET basic versions will not be garbled because the server encoding formats can be automatically converted url

 

POST garbled

 

Since the client is

UTF-8 character encoding to transmit the form data to the server, the server receives also set in UTF-8 character encoding, and reception parameters must be defined before;

 

// set the request parameter encoding format - on GET invalid

request.setCharacterEncoding("UTF-8");

 

Servlet output Chinese

 

1 page return garbled reasons

 

Browser can not recognize what is returned by the Chinese encoding format, will be used by default

GB2312, if the return is UTF-8 format will be displayed during garbage problem in the browser

 

 

2 How to solve content garbled

response.setContentType("text/html;charset=UTF-8");

 

Comprehensive set of 3

 

Prior to receiving the parameter values:

 

request.setCharacterEncoding ( "UTF-8"); // set request encoding

response.setCharacterEncoding ( "UTF-8"); // set the response code

response.setContentType ( "text / html; charset = utf-8"); // set the response in response to the type and content

 

Coding

 

 

 

 



 

Guess you like

Origin www.cnblogs.com/qfchen/p/11590449.html