The problem of Chinese garbled in web development and its solution

Source: Internet
Author: User
Tags character set config tomcat tomcat server

In the Web application development process, often encounter the problem of Chinese garbled, the following is a common cause of Chinese garbled problems and solutions (Tomcat)

1.JSP page contains Chinese cannot be saved, the hint in eclipse should be saved as UTF-8

The workaround is to add page directives to the JSP page as follows:

<%@ page language= "java" pageencoding= "UTF-8"%>

Or

<%@ page language= "java" pageencoding= "GBK"%>

2. The JSP page is in Chinese, but the page is garbled:
The workaround is to add the page directive to the JSP page, as follows:

<%@ page language= "java" contenttype= "Text/html;charset=utf-8" pageencoding= "UTF-8"%>

Here's what pageencoding and contenttype mean.

Pageencoding: Indicates the encoding format of the JSP source file. That is, what code this JSP uses to save on disk.

ContentType CharSet refers to the content encoding when the server is sent to the client

JSP to pass two times "coding", the first stage will use Pageencoding, the second stage will use Utf-8 to Utf-8, the third stage is the JSP generated Web page, with the ContentType.

The first stage is that the JSP is compiled into Java

It will read the JSP according to the pageencoding settings, the results are translated into a unified UTF-8 Java source code (ie. java), if the pageencoding set wrong, or not set, out of the Chinese garbled.

The second stage is compiled by Javac Java source to Java bytecode

No matter what coding scheme is used when JSP is written, in the first stage it transforms into the Java source of UTF-8 encoding.

Javac uses UTF-8 's encoding to read Java source code, compiled into a UTF-8 encoding binary (i.e.. Class), which is the JVM's specification of a constant number string expressed within a binary code (Java encoding).

The third stage is the Java binary code of the Web container loading and executing phase two

The result of the output, which is seen at the client, is encoded using the encoding specified by the parameter contenttype hidden in phase one and phase two.

3. When you use the Request object to get customer submitted form parameters that contain Chinese, garbled characters appear:
The solution is: To configure a filter, which is a servlet filters, the code is as follows:
Import java.io.IOException;
Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import javax.servlet.UnavailableException;

/**
* Example filter, sets the character encoding to being used in parsing the
* Incoming Request
*/
public class Setcharacterencodingfilter implements Filter {

/**
* Take this filter out of service.
*/
public void Destroy () {
}
/**
* Select and set (if specified) the character encoding to being used to
* Interpret request parameters for this request.
*/
public void DoFilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {

Request.setcharacterencoding ("GBK");//or Utf-8

Transfer control to next filter
Chain.dofilter (request, response);
}

public void init (Filterconfig filterconfig) throws Servletexception {
}
}
Configure Web. xml
< filter>
< Filter-name>set Character encoding</filter-name>
< filter-class>setcharacterencodingfilter</filter-class>
</filter>
< filter-mapping>
< Filter-name>set Character encoding</filter-name>
< url-pattern>/*</url-pattern>
</filter-mapping>
If you still appear in Chinese garbled, this situation, you can look down to see if you appear in the fourth situation, the data submitted by your form is not submitted with get, generally using post to submit the words are no problem, if so, you look at the fourth solution.
There is the processing of information containing Chinese characters, the code to be processed is:
Package Dbjavabean;

public class Codingconvert
{
Public Codingconvert ()
{
//
}
public string Togb (string unistr) {
String gbstr = "";
if (unistr = = null) {
Unistr = "";
}
try{
byte[] Tempbyte = unistr.getbytes ("Iso8859_1");
Gbstr = new String (tempbyte, "GB2312");
}
catch (Exception ex) {
}
return gbstr;
}

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 direct the conversion, first you will get the string encoded with iso-8859-1, and then put the code into a byte array, and then convert the array into a string object can be, for example:
String str=request.getparameter ("Girl");
Byte b[]=str.getbytes ("iso-8859-1");
Str=new String (B);
Any information submitted will be displayed correctly through the above conversion.
(iii) Requests are requested on the service side at the Form#get. GetParameter ("name") is garbled; setting filter by Tomcat does not work or uses request.setcharacterencoding ("GBK"); Also, the problem is in handling the method of parameter passing: if the doget (HttpServletRequest request, HttpServletResponse Response) method is used in the servlet, the front is even written:
Request.setcharacterencoding ("GBK");
Response.setcontenttype ("TEXT/HTML;CHARSET=GBK");
Also do not work, the return of Chinese or garbled ... If you change this function to Dopost (httpservletrequest request, httpservletresponse response) everything is OK.
Similarly, processing form input with two JSP pages can be displayed in Chinese because the post method is passed, and the Get method is still not available.
This shows the use of the Doget () method in the servlet or in the JSP with the Get method to handle the attention. This, after all, involves passing parameter information through a browser, which is likely to cause conflicts or mismatches between common character sets.
The solution is to:
1) Open the Tomcat Server.xml file, locate the chunk, and add the following line:
Uriencoding= "GBK"
The complete should be as follows:
<connector port= "8080" maxthreads= "All" minsparethreads= "" maxsparethreads= "" enablelookups= "" false " Redirectport= "8443" acceptcount= "debug=" 0 "connectiontimeout=" 20000 "disableuploadtimeout=" true "URIEncoding=" GBK "/>

2) reboot Tomcat, all OK.
Need to join the reasons for everyone to study $TOMCAT _home/webapps/tomcat-docs/config/http.html under the file can know why. Note: This place if you use UTF-8 in the process of transmission in Tomcat is also to appear garbled, if not, then change the other character set.

(d) The JSP page has Chinese, the button above also has Chinese, but through the server to view the page appears garbled:
The workaround is that the JSP file should not contain the localized message text directly, but rather the text should be obtained from the resource bundle through the <bean:message> tag. You should put your Chinese text in the Application.properties file, this file is placed under web-inf/classes/*, for example, I have a name on the page, age two label,  My first is to build a application.properties, the content should be Name= "name" age= "age", and then I put this file under Web-inf/classes/properties/, followed by Application.properties file, encode and convert it, create a Chinese resource file, assuming the name is application_cn.properties. The NATIVE2ASCII command is available in the JDK to enable the conversion of character encodings. In a DOS environment, locate the directory where you placed the Application.properties file, execute commands in a DOS environment, and generate a Chinese resource file that is encoded by GBK Application_ Cn.properties:native2ascii? Encoding GBK application.properties Application_ Cn.properties execution of the above command will generate the following application_cn.properties file: name=\u59d3\u540d age=\u5e74\u9f84, Configuration in Struts-config.xml: <message-resources parameter= "Properties. Application_cn "/>. In this step, basically completed more than half, and then you have to write on the JSP page <%@ page language= "java" contenttype= "TEXT/HTML;CHARSET=GBK"%&GT;, to the name of the label is to write <bean:message key= "Name", such a page appears on the time will appear in the Chinese name, age this is the same, the button on the processing of Chinese characters is the same.
(v) Writing to the database is garbled:
WORKAROUND: To configure a filter, which is a servelet, the code is the same as the second.
If you are directly linking to the database through JDBC, the configuration code is as follows: JDBC:MYSQL://LOCALHOST:3306/WORKSHOPDB?USEUNICODE=TRUE&AMP;CHARACTERENCODING=GBK, This ensures that the code in the database is not garbled.
If you are linked through a data source you cannot follow this notation, first you will write in the configuration file, where the data source is configured in Tomcat 5.0.19 under C:\Tomcat 5.0\conf\catalina\localhost, I built the project is workshop, placed directory is webapp below, workshop.xml configuration file as follows:
<!--Insert this-Context element into Server.xml-

<context path= "/workshop" docbase= "workshop" debug= "0"
Reloadable= "true" >

<resource name= "Jdbc/workshopdb"
Auth= "Container"
Type= "Javax.sql.DataSource"/>

<resourceparams name= "jdbc/workshopdb";
    <parameter>
       <name>factory</name>
      <value> Org.apache.commons.dbcp.basicdatasourcefactory</value>
    </parameter>
     <parameter>
      <name>maxactive</name>
       <value>100</value>
    </parameter>
     <parameter>
      <name>maxidle</name>
       <value>30</value>
    </parameter>


<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>

<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value></value>
</parameter>

<!--Class name for Mm.mysql JDBC driver--
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
< value><! [cdata[jdbc:mysql://localhost:3306/workshopdb?useunicode=true&characterencoding=gbk]]></value>
</parameter>
</resourceparams>

</Context>

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.