Solution of Chinese issues under Tomcat (original)

xiaoxiao2021-03-06  64

These days develop a project, the server is Tomcat, the operating system is XP, which is the MVC architecture. The mode is used in the FACADE mode. It has always been garbled. I also solved it for many days. Colleagues also help solve, and more thanks to many netizens. Articles and opinions, it is finally available. But good memory is not as bad, so I deliberately, in order to prevent itself from forgotten, but also provide a good reference path to those who encounter the same problem:

(1) The JSP page is Chinese, but it is a garbled:

The solution is to the encoding of the JSP page <% @ page language = "java" contentty = "text / html; charSet = GBK"%> Is ISO-8859-1, if Chinese is directly entered in a JSP, JSP is handled as ISO8859-1 is definitely problematic, this, we can confirm by viewing JAVA intermediate files generated by Jasper

(2) When using the REQUEST object to obtain the Chinese character code submitted by the customer, garbled:

The solution is to configure a filter, which is a server filter, 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 THATS The Character Encoding to Be Used In Parsing The

* incoming request

* /

Public class setcharacterencodingfilter imports filter {

/ **

* Take this filter out of service.

* /

Public void destroy () {

}

/ **

* Select and set (if specified) The character encoding to be used to

* Interpret Request Parameters for this Request.

* /

Public Void Dofilter (ServletRequest Request, ServletResponse Response,

Filterchain chain) throws oException, servletexception {

Request.SetCharacterencoding ("GBK");

// Transfer control to the next filter

Chain.dofilter (Request, Response);

}

Public void init (filterconfig filterconfig) throws servletexception {

}

}

Configure web.xml

Set Character Encoding

setcharacterencodingfilter

Set Character Encoding

/ *

If you still have this situation, you will see if you have a fourth situation, your Form submitted data is not submitted by get, in general, if you submit it, there is no problem. If so, you will see the solutions solved in the fourth.

There is also the information containing the Chinese characters, and the code processing 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 transform directly, first of all, you will be encoded with ISO-8859-1, then store this encoding into a byte array, then convert this array into a string object, for example :

String str = Request.getParameter ("girl");

Byte b [] = str.getbytes ("ISO-8859

-1");

Str = new string (b);

By the above conversion, any information submitted is displayed correctly.

(3) Returning in the server-only request. GetParameter ("Name") is garbled; Press Tomcat to set Filter and use Request.SetCharacterencoding ("GBK"); no matter what the problem is On the method of processing parameters: If you have a DOGET (HTTPSERVLETREST, HTTPSERVLETRESE RESPONS) method, even write: Request.SetCharacterencoding ("gbk"); response.setContentType ("text / html; charSet = GBK "); also does not work, return Chinese or garbled! ! ! If this function is changed to dopost (httpservletRequest request, httpservletResponse response) everything is OK. Similarly, the meaning of the display can be displayed in two JSP page processing forms is because the POST method is transmitted, it is still not possible to change to the GET method. This shows that in the servlet uses a doget () method or in the JSP to use the GET method to handle it. This is, involving passing parameter information to pass through the browser, which is likely to cause conflicts of common character sets or do not match. The solution is:

1) Open Tomcat's server.xml file, find blocks, join the following line: uriencoding = "GBK" completely as follows:

2) Restart Tomcat, everything OK.

Reasons for need to join you can find this file under the Tomcat_Home / WebApps / Tomcat-DOCS / Config / Http.html. It should be noted that this place If you use UTF-8, you should have garbled in the Tomcat during the transfer, if you don't work, you will change your character set.

(4) There are Chinese on the JSP page, and there are Chinese on the button, but there is a garbled when viewed by the server:

The solution is: First, you should not directly contain localized message text in the JSP file, but should be obtained from the Resource Bundle from the tab. 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 in the page, two Label, I am first to build an Application.properties, The content inside should be name = "Name" age = "age", then put this file into web-inf / classs / proties / down, next to the Application.properties file, coding him, create a Chinese Resource file, assume that the name is Application_CN.properties. The native2ascii command is provided in JDK, which can implement the conversion of character encoding. In the DOS environment, find the directory of this file you place Application.properties, execute the command in the DOS environment, will generate the Chinese resource file application_cn.properties :native2ascii -Encoding GBK Application.properties Application_Cn.properties execute the above command Aplication_cn.properties files that will be generated later: Name = / U59D3 / U540D AGE = / U5E74 / U9F84, configured in struts-config.xml: . To this step, basically completed a half, then you will write <% @ page language = "java" contenttype = "java" contentty = "text / html; charset = GBK"%>, the Label to the name is to Write This will appear in the page when the page appears, the age is also the same, the processing of Chinese characters on the button is also the same.

(5) Write to the database is garbled:

Solved method: To configure a filter, it is a Servelet filter, the code is as in the second time.

If you are directly linking the database through the JDBC, the configuration code is as follows: JDBC: mysql: // localhost: 3306 / Workshopdb? Useunicode = true & characterencoding = GBK, this is guaranteed that the code in the database is garbled.

If you are linking through the data source, you can't follow this way, first you have to write in the configuration file, in Tomcat

5.0.19 Configuring the data source in C: / Tomcat 5.0 / conf / Catalina / Localhost, I created the project is Workshop, the list of placed is WebApp, the configuration file of Workshop.xml is as follows:

Reloadable = "True">

TYPE = "javax.sql.datasource" />

factory

org.apache.commons.dbcp.basicDataSourceFactory

maxactive

100

maxidle

30

maxwait

10000

Username

root

Password

driverclassname

com.mysql.jdbc.driver

URL

The bold is particularly paying attention to the difference between the Direct link of the JDBC. If you are configuring the correctization, when you enter Chinese, it is Chinese in the database, one thing to pay attention to you are displaying The data of the data is also to use <% @ page language = "java" contenttype = "text / html; charset = GBK"%> This line of code. It should be noted that some of the front desk is written in writing code. When writing a Form, he changed him to a JSP, so there is a place to pay attention, that is, in Dreamver's Action The way is Request, you need to take him, because in the process of JSP's submission, it is POST and GET in two ways, but the code submitted in these two ways is still very different in the coding, this is The later places will be described. 3

转载请注明原文地址:https://www.9cbs.com/read-89934.html

New Post(0)