Ssh2+mysql Chinese garbled solution (Unified using UTF-8 encoding)
Chinese garbled, first to distinguish is the page garbled, action garbled, or database garbled . The general principle is that Java uses Unicode encoding –>window to use the GBK (gb2312 extension Set) –mysql the default use of Utf-8 (a Unicode encoding method), so that the spin is garbled ^_^. Here's how to fix it:
1. In Struts2, it is best to set all characters to Utf-8.
<%@ page contenttype= "text/html; Charset=utf-8″%> <%@ page pageencoding= "utf-8″%> |
1.1 Set the character encoding on the JSP page. Here there must be explained is if it is Jsp+java bean+servlet scheme, Chinese garbled very good solution, unified set to gb2312 on it.
1.2the use of the Struts framework character set cannot be set to gb2312 and is changed to Utf-8. 2. Add in Struts.properties:
Struts.devmode=false Struts.enable.dynamicmethodinvocation=true Struts.i18n.reload=true Struts.ui.theme=simpleStruts.locale=zh_cn Struts.i18n.encoding=utf-8 Struts.serve.static.browsercache=false Struts.url.includeparams=none |
where locale, encoding is the setting of the character set.
3. Add a filter[in the Web. Xml in the Struts2 frame, the filter must be placed before the action's filter! Order is very important!!! ]
<!–ZH-CN encoding–> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.sterning.filter.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value><!--or the encoding format you need-- </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
Similar to the above method, you can also set character characters in the action.
HttpServletResponse response = null; Response = Servletactioncontext.getresponse (); Request.setcharacterencoding ("Utf-8″"); Response.setcontenttype ("Text/html;charset=utf-8″"); |
Through the above method, the basic can solve the problem of Chinese garbled. Of course, there are exceptions (such as the version of Web server \ Database version, and so on). Like in one of my projects encountered a Chinese garbled, tomcate5.5 will be garbled, and in the Tomcate6 will not. This is related to the setting of the Tomcate connector character.
<connector port= "80″maxhttpheadersize=" 8192″ maxthreads= "150″minsparethreads=" 25″maxsparethreads= "75″ Enablelookups= "false" redirectport= "8443″acceptcount=" 100″ Connectiontimeout= "20000″disableuploadtimeout=" true " uriencoding=" GBK " /> |
——————————————————————–
One of the PostScript: when using STRUTS2, still encountered a garbled. Later debugging only found that thestruts2 Web. XML configuration is sequential .
The location of Encodingfilter in Web. XML should precede the Filterdispatcher of Struts2, because the character set is first adjusted and then moved to action.
The order of Api,filter according to Struts2 is
Struts-cleanup Filter
Sitemesh Filter
Filterdispatcher
——————————————————————–
PostScript Two: This method is the next worst, only in the previous method is invalid when used.
The Request.getparameter () is used directly in the action, or garbled. The causes are analyzed as follows:
1, GetParameter () is a character parameter. Cases:
string s = (string) request.getparameter ("TXT"). getBytes ("iso-8859-1");
2, string can also have character parameters.
String(byte[] bytes, String charsetName)
Constructs a new Stringby decoding the specified byte array using the specified character set.
Example: string s = new string ("Chinese", "Utf-8″");
3, synthesis of the above two points, write a class to complete this task
public class convertcharacter{ public string Convert (string s) { String result; Byte[] temp; try{ temp = s.getbytes ("Iso-8859-1″"); result = new String (temp, "Utf-8″"); } return result; } |
Finally, the database is set up:
In the case of MYSQL5, create DB and table should be set CharSet to gbk!
Summarize:
In short, you must ensure that your characters go all the way to use the same code, preferably UTF-8.
The JSP page shown is <%@ page contenttype= "text/html; Charset=utf-8 "language=" java "%>
Struts2 set <constant name= "struts.i18n.encoding" value= "UTF-8"/>
Filter the characters with a UTF-8
Database MySQL created library to be encoded with UTF-8
Note below:
When configuring Hibernate in SPRING2 (MySQL database), the connection string must be marked with character encoding, otherwise it is definitely garbled. For example, my:
<bean id= "DataSource"
class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "Driverclassname"
Value= "Org.gjt.mm.mysql.Driver" >
</property>
<property name= "url" value= "Jdbc:mysql://localhost:3306/mydata?useunicode=true& Characterencoding=utf-8 "></property>
<property name= "username" value= "root" ></property>
<property name= "Password" value= "root" ></property>
</bean>
Look closely at the top of the red marked part is "&", can not be written as "&" symbol.
That's where it all worked.
Tomcat+ssh+mysql local normal, remote server Chinese garbled. Go