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
filter-mapping>
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 (httpservletResponse response method in servlet), even if you write:
Request.SetCharacterencoding ("GBK");
Response.setContentType ("text / html; charset = GBK");
Also don't 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"
Complete should be 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
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 through the data source link, you can't follow this way, first you have to write in the configuration file, configure the data source in Tomcat 5.0.19 is in C: / Tomcat 5.0 / Conf / Catalina / Localhost, the project I built is Workshop, the directory placed is WebApp, the configuration file of Workshop.xml is as follows:
Reloadable = "True"> TYPE = "javax.sql.datasource" /> parameter> parameter> parameter> parameter> parameter> parameter> parameter> parameter> Resourceparams> Context> 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