Tomcat's garbled problem

Source: Internet
Author: User

String Filename=request.getparameter ("filename"); Filename=new string (Filename.getbytes ("iso8859-1"), "Utf-8"); Explain the second sentence, what is the intention? Why did you do that? What is the principle?
1. This is often the case when the HTTP request get transfer parameter mode, the Web container is Tomcat, and Tomcat's default encoding is iso8859-1. 2, if the iso8859-1, and your project is not the code in the reception of Chinese characters, it will appear garbled. 3, the solution, that is, the conversion of iso8859-1 into a project code such as GBK or UTF-8, the transfer process to solve the problem of Chinese garbled.
Not sure when to deal with this? When should I not?
 1, in the GET request, the key to see your tomcat in the Server.xml "<connector port=" 8080 "protocol=" http/1.1 "Connectiontimeou t= "20000" redirectport= "8443"/> "This connector is configured uriencoding value, do not configure the default is Iso8859-1, if written like Utf-8, your program will not need to go, if you still Turn instead of garbled. 2, in the POST request, do not consider the point, as long as write a character filter all the cloth set to Utf-8, you can avoid garbled. These need to be tested over and over again to have a deeper understanding. 

HTTP requests are iso-8859-1 encoded to transmit the URL if the Content-type of the page is Utf-8, then when the request is sent, the character will be transferred to Utf-8 after it is transferred such as: UTF-8 E4 the ad is B8 on request Ad bytes are% separated then the server receives this byte stream, it must be converted to the corresponding character, the usual Request.getparameter ("name") to get the string directly, then from the stream of bytes to the word stream the process system has been helped us to complete ( This starts with garbled characters) similar to the following program: Convert the UTF-8 form of Chinese character "Zhong" into Chinese public class Encodingtest {public static void main (string[] args) {String utf_ string = "E4%b8%ad";//UTF-8 encoding, three bytes, separated by% string[] Utf_array = utf_string.split ("%"); byte[] Utf_byte = new Byte[utf_ array.length];for (int i=0;i<utf_array.length;i++) {Utf_byte[i] = (byte) integer.parseint (Utf_array[i], 16);} try {System.out.println (new String (Utf_byte, "UTF-8")),} catch (Unsupportedencodingexception e) {//TODO auto-generated Catch Blocke.printstacktrace ();}}} Note The segment code: new String (Utf_byte, "UTF-8") that restores the byte stream to UTF-8 encoding as a string, but it uses the precondition that utf_byte must be a byte stream of utf-8, if so: String AA = "medium"; bb = Aa.getbytes ("Iso-8859-1"); new String (BB, "UTF-8") must be garbled. This means that a string is converted into a byte stream with what encoding it has to be restored bb=aa.getbytes (" UTF-8 ") must be new String (BB," UTF-8 ") otherwise garbled: URL through iso-8859-1 transmission, the server received this byte stream,By default, this byte stream is reverted to the corresponding string as Iso-8859-1: the client: BB = aa.getbytes ("UTF-8"), to the server: CC = new String (BB, "iso-8859-1") The restore byte stream is a section character string Request.getparameter (""); get to this cc, then CC is of course garbled, so there is the practice: New String (Request.getparameter (""). GetBytes (" Iso-8859-1 ")," UTF-8 ") reverts to the original string, restores the garbled string to the original byte stream, and re-presses the UTF-8 to encode. This is because
Request.getparameter ("") is actually a iso-8859-1 decoding, and then get a garbled string, then you have to re-press ISO-8859-1 encoded into a byte stream, and then in the
String (BB, "UTF-8") is decoded as a character stream by UTF-8.
As for changing uri-encoding in Tomcat and using filters, I'm guessing to change the encoding of bytes into strings
Here is just my point of view, wrong, please correct me

Tomcat's garbled problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.