Solve the Problem of passing garbled values between JSP pages

Source: Internet
Author: User
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!

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.