The ultimate solution for JSP and Servlet Chinese garbled characters

Source: Internet
Author: User

The ultimate solution to JSP Chinese garbled characters

1. Identify the root cause of the problem
Where garbled characters may occur: 1 JSP page
2. Parameters for passing parameters between JSP pages
3. Access to data in the database
These types are summarized as follows.

2. Find a solution
1 appears on the JSP page because the Chinese character encoding of the JSP page is not set.
2. parameters are transmitted between JSP pages because the correct character encoding is not set for the parameters.
3. If the above two problems are solved, there will naturally be no garbled characters when they are stored in the database. Unless you encode the data stored in the database again.
Solution 3:
Solution 1
<%
@ Page contenttype =
"
Text/html; charset = gb2312
"
%>
After Tomcat is compiled, the HTML file output to the client is not collected.

It is encoded in Chinese, so garbled characters are generated.

Solution 2
2.1 <%
Request. setcharacterencoding (
"Gb2312
");
%>
Add this sentence to solve the Chinese parameter passing garbled characters on the JSP page.
Because the browser uses the default encoding "UTF-8" to send request parameters.
We can change it to "gb2312.

2.2 string (request. getparameter ("name"). getbytes ("iso8859_1"), "gb2312 ");
This statement is used to encode all incoming parameters into gb2312. The disadvantage of this statement is that a parameter is sent every time.

It is very troublesome to write in this way.

You can also set the server. xml configuration file.
<Connector
Port = "8080"
Maxhttpheadersize
= "8192"
Maxthreads = "150"
Minsparethreads = "25"
Maxsparethreads = "75"

Enablelookups = "false"
Redirectport = "8443"
Acceptcount = "100"

Connectiontimeout = "20000"
Disableuploadtimeout
= "True"
Uriencoding = "gb2312"
/>
In this way, the application is applied to the entire webapp.

In addition: <%
@ Page pageencoding =
"
Gb2312"
%>
To enable the JSP compiler to correctly decode the JSP page containing Chinese characters.

Other methods can also modify the Web. xml file and configure a filter. The principles are the same, just in a different way.
Some books specifically write a function to solve Garbled text. In fact, we can see the good and bad solutions.
Looking back, we can see that this is also true for solving Garbled text.
OK. Add the three sentences to solve the problem.
<%
@ Page pageencoding
="
Gb2312"
%>
<%
@ Page contenttype
="
Text/html; charset = gb2312
"
%>
<%
Request. setcharacterencoding (
"
Gb2312"
); %>

 

Servlet-related garbled characters

 

 

When I deploy a WebServices program today, a garbled error occurs in the servlet that obtains data from the page. In the servlet, I have configured the request. setcharacterencoding ("gb2312"); this text is added to the code, but it is still messy.
Find information online, as follows: Java is Unicode encoding, you first convert to ISO8859-1, and then convert to GBK or gb2312.
Java code
  1. Request. setcharacterencoding ("ISO8859-1 ");
  2. AD = request. getparameter ("name ");
  3. Byte [] temp3 = AD. getbytes ("GBK ");
  4. String STR = new string (temp3 );
This is Chinese! Another simpler method is to set the encoding and conversion directly on the server. Tomcat 5.0.28 is used.
Find the server. xml file in the. \ Tomcat 5.0 \ conf directory and open it with a WordPad. Set it as follows: You can add an attribute to it. uriencoding = "GBK"
Test passed, done, Then, what is the role of the request. setcharacterencoding ("gb2312") statement? If it cannot solve the problem of chaos, what is the role we use it,Sort the information as follows:
(1) Chinese characters are displayed on the JSP page, but garbled characters are displayed:
The solution is in the JSP page encoding, because JSP into Java file encoding problem, some of the default server is ISO-8859-1, if a JSP directly entered the Chinese, JSP treats it as a ISO8859-1 and there is certainly a problem, which we can confirm by viewing the Java intermediate file generated by Jasper

(2) garbled characters occur when the request object is used to obtain the Chinese character code submitted by the customer:
The solution is to configure a filter, that is, a servelet filter. The Code is as follows:
Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {

Request. setcharacterencoding ("GBK ");

// Transfer control to the next Filter
Chain. dofilter (request, response );
}
Configure web. xml
<Filter> </filter>
<Filter-Name> </filter-Name> set character encoding
<Filter-class> </filter-class> setcharacterencodingfilter

<Filter-mapping> </filter-mapping>
<Filter-Name> </filter-Name> set character encoding
<URL-pattern> </url-pattern> /*

If you still see this situation, you can see if you have encountered the fourth situation and whether the data submitted by your form is submitted using get, in general, there is no problem in submitting a post statement. If so, let's take a look at the solution in step 4.
 
The following code processes the information containing Chinese characters:
Public String touni (string gbstr ){
String unistr = "";
If (gbstr = NULL ){
Gbstr = "";
}
Try {
Byte [] tempbyte = gbstr. getbytes ("gb2312 ");
Unistr = new string (tempbyte, "iso8859_1 ");
} Catch (exception ex ){
}
Return unistr;
}
}
You can also directly convert it, first you encode the obtained string with the ISO-8859-1, and then store the encoding into a byte array, then convert the array into a string object. For example:
String STR = request. getparameter ("girl ");
Byte B [] = Str. getbytes ("ISO-8859-1 ");
STR = new string (B );
Any submitted information can be correctly displayed after the preceding conversion.
(3) The formget request is used on the server. when getparameter ("name") is returned, garbled characters are returned. It is useless to set Filter Based on tomcat or request. setcharacterencoding ("GBK"); also, the problem lies in the method for passing the processing parameters: If doget (httpservletrequest request, httpservletresponse
If the response method is used for processing, the preceding statements are written as follows:
Request. setcharacterencoding ("GBK ");
Response. setcontenttype ("text/html; charset = GBK ");
It does not work either. The returned Chinese characters are still garbled !!! If you change this function to dopost (httpservletrequest request, httpservletresponse response), everything will be OK.
Similarly, when two JSP pages are used to process form input, the reason why Chinese characters can be displayed is that the POST method is used for transmission, and the get method cannot be used.
It can be seen that you should pay attention to using the doget () method in servlet or the get method in JSP. After all, it involves passing parameter information through the browser, which may cause conflicts or mismatched common character sets.
// This part is understood as request. setcharacterencoding ("GBK"); set the body in the request, not the header part. When a GET request is sent, the parameter is placed behind the URL, not in the body. Therefore, the request is sent at this time. setcharacterencoding ("GBK") does not play a role. If it is changed to post, there will be no problem. After testing,

The solution is:
1) Open the Tomcat server. xml file, locate the block, and add the following line:
Uriencoding = "GBK"
The complete information should be as follows:
<Connector uriencoding = "GBK" maxthreads = "150" DEBUG = "0" redirectport = "8443" Port = "8080" enablelookups = "false" maxsparethreads = "75" minsparethreads =" 25 "connectiontimeout =" 20000 "disableuploadtimeout =" true "acceptcount =" 100 "> </connector>

2) Restart tomcat. Everything is OK.
Tomact.org
This specifies the character encoding used to decode the URI bytes, after % XX decoding the URL. if not specified, ISO-8859-1 will be used. that is to say, the bytes and request of Uri are converted. setcharacterencoding ("GBK") converts the request. The problem is OK!

(4) The JSP page contains Chinese characters and buttons, but garbled characters appear when you view the page through the server:
Solution: first, the JSP file should not directly contain localized message text, but be obtained from the resource bundle using the <Bean: Message> tag. Put your Chinese text in application. in the properties file, this file is placed in the WEB-INF/classes/*, for example, I have a name in the page, the age of two labels, I first want to create an application. properties, the content should be name = "name" age = "Age", then I put this file under the WEB-INF/classes/properties/, then according
Application. properties file, encode and convert it, and create a Chinese resource file, assuming the name is application_cn.properties. The native2ascii command is provided in JDK to convert character encoding. In the DOS environment, find the directory of the file where you placed application. properties. In the DOS environment, execute the command to generate a GBK-Encoded chinese resource file application_cn.properties: native2ascii? Encoding GBK application. Properties
Application_cn.properties the application_cn.properties file: Name = u59d3u540d age = u5e74u9f84 will be generated after the above command is executed, configure in Struts-config.xml: <message-resources parameter = "properties. application_cn "> </message-resources>. In this step, we have basically finished more than half of it, and then you will write it on the JSP page. The label of the name is to write <Bean: Message
Key = "" name ""> when such a word appears on the page, the Chinese name is displayed. The same is true for age, and the processing of Chinese characters on the button is the same.
(5) garbled characters are written to the database:
Solution: configure a filter, that is, a servelet filter. The Code is the same as in the second case.
If you directly connect to the database through JDBC, the configuration code is as follows: JDBC: mysql: // localhost: 3306/workshopdb? Useunicode = true & characterencoding = GBK to ensure that the code in the database is garbled.
If you are using a data source link, you cannot follow this method. First, you need to write it in the configuration file. In Tomcat 5.0.19, the data source is configured in C: under Tomcat 5.0confcatalinalocalhost, the project I created is workshop, and the directory is under webapp, workshop. the xml configuration file is as follows:

<Context DEBUG = "0" Path = "/Workshop" docbase = "Workshop"> </context> reloadable = "true">

<Resource Name = "JDBC/workshopdb"> </resource> auth = "Container"
Type = "javax. SQL. datasource"/>

<Resourceparams name = "JDBC/workshopdb"> </resourceparams>
<Parameter> </parameter>
<Name> </Name> Factory
<Value> </value> org. Apache. commons. DBCP. basicperformancefactory

<Parameter> </parameter>
<Name> </Name> maxactive
<Value> </value> 100

<Parameter> </parameter>
<Name> </Name> maxidle
<Value> </value> 30

<Parameter> </parameter>
<Name> </Name> maxwait
<Value> </value> 10000

<Parameter> </parameter>
<Name> </Name> Username
<Value> </value> root

<Parameter> </parameter>
<Name> </Name> Password
<Value> </value>

<Parameter> </parameter>
<Name> </Name> driverclassname
<Value> </value> com. MySQL. JDBC. Driver

<Parameter> </parameter>
<Name> </Name> URL
<Value> </value>

Special attention should be paid to the bold text, which is different from the JDBC direct link. If you are configuring correctly, when you enter Chinese, it will become Chinese in the database, one thing to note is that you also need to use this line of code to display the data page. It should be noted that some front-end staff use dreamver to write code after writing a form, and change it to a JSP when writing a form, so there is a place to pay attention, that is, in dreamver, the action submission method is request, and you need to submit it, because in the JSP submission process, there are two methods: Post and get, however, the Code submitted by the two methods is quite different in coding, which will be explained later. </Bean: Message>

 

 

If you have any ideas or have any mistakes, please explain them and write them on the message for discussion.

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.