A project is developed these days. The server is tomcat, the operating system is XP, And the MVC Architecture is used. The mode is facade mode, and garbled characters are always displayed. By setting the page character set, at last, the Chinese can be correctly displayed, but I did not expect the Chinese characters in the data submitted in the form to be garbled. I was so dizzy that I did not expect the garbled problem in JSP to be much more serious than that in ASP, I have also solved the problem for many days, and my colleagues have helped me solve the problem. I have also referred to the articles and opinions of many netizens on the internet, and I have finally done it. However, a good memory is not as good as a bad pen, so you can write it down to prevent yourself from forgetting it. At the same time, you can also provide a good reference for those who encounter the same problem:
The following describes the methods on the Internet. (1) The JSP design page contains Chinese characters, but garbled characters are displayed during running: the solution is to encode the JSP page <% @ page Language = "Java" contenttype = "text/html; charset = GBK" % & gt ;, because JSP into Java file encoding problem, some of the default server is ISO-8859-1, if a JSP directly input the Chinese, JSP treats it as a ISO8859-1 to deal with is certainly a problem, in this regard, we can check the Java intermediate file generated by Jasper to confirm (2) when the request object is used to obtain the Chinese character code submitted by the customer, garbled characters will appear, such as in the form: the solution is to configure a filter, that is, a servelet filter. The Code is as follows: Import Java. io. ioexception; import javax. servlet. filter; import javax. servle T. filterchain; import javax. servlet. filterconfig; import javax. servlet. servletexception; import javax. servlet. servletrequest; import javax. servlet. servletresponse; import javax. servlet. unavailableexception; public class setcharacterencodingfilter implements filter {public void destroy () {} public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, Ser Vletexception {request. setcharacterencoding ("GBK"); // transfer control to the next filter chain. dofilter (request, response);} public void Init (filterconfig) throws servletexception {}} configure web. XML <filter> <filter-Name> set character encoding </filter-Name> <filter-class> COM. setcharacterencodingfilter </filter-class> </filter> <filter-mapping> <filter-Name> set character encoding </filter-Name> <URL-pattern>/* </URL -Pat Tern> </filter-mapping> if you still encounter this situation, you can check whether 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. In addition, you can process the information containing Chinese characters in the following code: Package dbjavabean; public class codingconvert {public codingconvert () {// process} 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) {// exception process} return gbstr ;} public String touni (string gbstr) {string Unis Tr = ""; if (gbstr = NULL) {gbstr = "";} Try {byte [] tempbyte = gbstr. getbytes ("gb2312"); unistr = new string (tempbyte, "iso8859_1");} catch (exception ex) {}return unistr ;}} you can also convert it directly, first you encode the obtained string with a ISO-8859-1, and then store the encoding in a byte array, and 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 is correctly displayed through 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 response) is used in the servlet) if the method is used for processing, the request is written. setcharacterencoding ("GBK"); response. setcontenttype ("text/html; charset = GBK"); does not work either. The returned Chinese characters are 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. Solution: 1) Open the Tomcat server. XML file, locate the block, and add the following line: uriencoding = "GBK" should be complete as follows: <connector Port = "8080" maxthreads = "150" minsparethreads = "25" maxsparethreads = "75" enablelookups = "false" redirectport = "8443" acceptcount = "100" DEBUG =" 0 "connectiontimeout =" 20000 "disableuploadtimeout =" true "uriencoding =" GBK "/> 2) restart tomcat. Everything is OK. You can find out the reason for adding the file $ tomcat_home/webapps/tomcat-docs/config/http.html. It should be noted that: this place if you use the UTF-8 in the Process of delivery in Tomcat is also the case of garbled, if not, it will change to another character set. (4) The JSP page contains Chinese characters and buttons, but garbled characters appear when you view the page through the server: first, the JSP file should not directly contain localized message text, but be obtained from the resource bundle using the <Bean: Message> label. 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 to the 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 after executing the preceding command, the application_cn.properties file: Name =/u59d3/u540d age =/u5e74/u9f84 will be generated and configured in the Struts-config.xml: <message-resources parameter = "properties. application_cn "/>. In this step, we have basically finished more than half of it. Then you need to write <% @ page Language = "Java" contenttype = "text/html; charset = GBK "%>, the label to the name is to write <Bean: Message key =" name ">, in this way, the Chinese name will appear on the page. 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: the solution is to 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.0/CONF/Catalina/localhost, the project I created is workshop, and the directory is under webapp, workshop. the xml configuration file is 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> <Name> maxactive </Name> <value> 100 </ Value> </parameter> <Name> maxidle </Name> <value> 30 </value> </parameter> <Name> maxwait </Name> <value> 10000 </value> </parameter> <Name> username </Name> <value> root </value> </parameter> <name> password </Name> <value> </parameter> <! -- Class name for mm. mySQL JDBC driver --> <parameter> <Name> driverclassname </Name> <value> COM. mySQL. JDBC. driver </value> </parameter> <Name> URL </Name> <value> <! [CDATA [JDBC: mysql: // localhost: 3306/workshopdb? Pay special attention to useunicode = true & amp; characterencoding = GBK]> </value> </parameter> </resourceparams> </context> in bold, it is different from 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 must use this line of code <% @ page Language = "Java" contenttype = "text/html; charset = GBK" %> On the page where the data is displayed. 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. 3. above is the problem of solving Chinese characters in the development system. I don't know if I can solve the problem of everyone. I am in a hurry and fail to improve it in time, and the writing is not very good. In some cases, it is estimated that words are not satisfactory. You can give me comments and hope to make progress together. Other methods above can be solved. In method (2), this filter is relatively simple. If the character set is different, you need to manually modify this filter. The following describes a filter with strong functions:
Package com. Manage. filter; import javax. servlet .*;
Import java. Io. ioexception;
Public class setcharacterencodingfilter implements filter {
Protected string encoding = NULL;
Protected filterconfig = NULL;
Protected Boolean ignore = true;
Public void destroy () {This. Encoding = NULL;
This. filterconfig = NULL ;}
Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain)
Throws ioexception, servletexception {
If (ignore | (request. getcharacterencoding () = NULL )){
String encoding = selectencoding (request );
If (encoding! = NULL)
Request. setcharacterencoding (encoding );
}
Chain. dofilter (request, response );}
Public void Init (filterconfig) throws servletexception {This. filterconfig = filterconfig;
This. Encoding = filterconfig. getinitparameter ("encoding ");
String value = filterconfig. getinitparameter ("Ignore ");
If (value = NULL)
This. Ignore = true;
Else if (value. inclusignorecase ("true "))
This. Ignore = true;
Else if (value. inclusignorecase ("yes "))
This. Ignore = true;
Else
This. Ignore = false ;}
Protected string selectencoding (servletrequest request) {return (this. Encoding) ;}// eoc/** set this in Web. xml
<Filter>
<Filter-Name> set character encoding </filter-Name>
<Filter-class>
Com. Manage. Filter. setcharacterencodingfilter
</Filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> UTF-8 </param-value>
</Init-param>
<Init-param>
<Param-Name> ignore </param-Name>
<Param-value> true </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> set character encoding </filter-Name>
<Servlet-Name> action </servlet-Name>
</Filter-mapping>
*/For method (2), add the following code to each page: <% request. setcharacterencoding ("gb2312"); %>
<% Response. setcharacterencoding ("gb2312"); %>
I heard this, but I tried to solve the problem of invalid Chinese garbled characters in the MySQL database. I have summarized a dedicated solution,
1. If there is garbled code on the JSP page, the first thing to do is to unify the aggregation set, for example, set them to utf8.
2. If the get parameter is garbled, set uriencoding in the server. xml configuration file as follows:
<Connector Port = "8080" protocol = "HTTP/1.1"
Connectiontimeout = "20000"
Redirectport = "8443"Usebodyencodingforuri = "true"Uriencoding = "utf8"/>
Uriencoding is only valid for get parameters.
According to people on the Internet, usebodyencodingforuri = "true" is designed to ensure the compatibility between Tomcat 4 and 5. This attribute is set for garbled post parameters. I tried it. As long as the subsequent steps are correctly set, this is dispensable.
3. Then I went to the filter settings that I liked on the Internet. Two key points:
First, set in the dofilter method of FilterRequest. setcharacterencoding ("utf8 ");This is to use utf8 for encoding before passing the parameter, and then pass it.However, this setting only appliesThe post parameter is valid.
Second,This filter must be placed first in the web. xml declaration, and must be prior to other filters.Otherwise, there is no effect. It may be related to the fact that the setcharacterencoding parameter set after the request is read is invalid.
4. Ensure that the above three items are set in sequence, so there should be no garbled characters. In addition, you can use the following methods to solve the problem:
<% = New string (request. getparameter ("username"). getbytes ("iso-8859-1"), "utf8") %>
Because Tomcat is based on ISO-8859-1 by default, so in utf8 JSP page input parameters, Tomcat will also be encoded by ISO-8859-1, and then passed to the next JSP page. Therefore, in the next JSP page, press the "anti-encoding" written above to get the correct utf8 value.
Chinese garbled characters in tomcat6 of IT Ninja Turtles