1, using GetParam and other methods to get request parameters encountered garbled
The encoding used by the request parameter that the browser sends is the encoding that is used when the Web page is opened. If the server side obtains the request parameter which sends over, by default uses the Iso8859-1 to decode the operation, the Chinese must have the garbled problem.
The data submitted by the Post and get methods are differentiated:
For data submitted by post, request.setcharacterencoding ("gb2312"), which explicitly specifies the use of encoding when fetching request parameters. Note that only the Post method is valid.
For Get mode, you can only manually decode string newName = new String (Name.getbytes ("iso8859-1"), "gb2312"), encode and decode again, this method is also valid for post. In Tomcat's
The uriencoding that can be configured in Server.xml can specify the encoding that the server uses by default when it obtains the request parameters, and then once and for all refuse to get the garbled problem of the request parameter. You can also specify
The Usebodyencodingforuri parameter, so that request.setcharacterencoding also works with the Get mode request, but because the publishing environment generally does not allow the modification of this property, try not to use it. Get mode is more complex to handle.
So the JSP uses post as much as possible to submit the data.
2. Garbled when using request to output data to client
The browser uses the system default platform bytecode to open the data sent by the server, if the server side uses non-platform code to output character byte data, it is necessary to explicitly specify the code table used by the browser encoding to prevent garbled problems.
Response.AddHeader ("Content-type", "text/html;charset=gb2312"); Response.getwriter (). Write ("Chinese"); output data, which is a stream of characters, Response this character will be transferred after transcoding operation
Out to the browser, this process by default using the Iso8859-1 code table, and iso8859-1 in the absence of Chinese, so the transcoding process in lieu of Chinese, resulting in garbled problems. You can specify the target code table used by the response during transcoding to prevent confusion
Code. Response.setcharcterencoding ("gb2312"), in fact response also provides a setcontenttype ("text/html;charset=gb2312") method, more convenient.
Resolving garbled issues when transferring data between a Web browser and a servlet