Cause of garbled characters: When Java is transmitted on the network, it is transmitted in the form of iso-8859-1 encoding. Therefore, when encoding and decoding (to obtain the passed value), it is necessary to match ..
The solution is as follows:
Method 1: transcode new string (U. getbytes ("iso-8859-1"), "gb2312 ");
Example:
String username = requset. getparameter ("username ");
Username = new string (username. getbytes ("iso-8859-1"), "gb2312 ");
However, it is still too troublesome to perform this operation every time. Therefore, you can write a tool class to convert garbled characters into gb2312.
Public class tools {
// Provides a method to convert garbled characters into gb2312
Public static string getnewstring (string input ){
String result = "";
Try {
Result = new string (input. getbytes ("iso-8859-1"), "gb2312 ");
} Catch (exception e ){
E. printstacktrace ();
}
Return result;
}
In use,
String username = request. getparameter ("username ");
Username = tools. getnewstring (U );
Note: When this method is used, it is not necessarily followed by gb2312, which may be GBK or UTF-8. The specific encoding of the page that transmits values to the controller depends on the encoding.
Method 2:. [unstable] by configuring the server. xml file
1. Some people say this sentence in the configuration file server. XML for Tomcat:
<Connector uriencoding = "gb2312"
Port = "8080" maxhttpheadersize = "8192"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" redirectport = "8443" acceptcount = "100"
Connectiontimeout = "20000" disableuploadtimeout = "true"/>
Uriencoding = "gb2312"
2. Although yes, there are prerequisites. If your filter is not configured,
The server. xml file is configured, so that it is only available when the form is get to pass the value!
When it is post, it is still garbled!
* In this case, we can see that the post and get methods in tomcat5 are different *
* Is there any way to make the get or POST method useful for transferring values,
Use the following method 3 *
Method 3:
1. Configure the filter and the server. xml file, both of which are encoded as gb2312.
2. In this way, neither post nor get form-based values will be garbled!