Recently took over a mobile Terminal Server program (Java implementation), my side of the test, the front desk directly use the Get method to transmit data,
Get request encoding method and Post request submit encoding method, get is to put the data directly into the URL, for example, the above Uname,ie browser first utf-8 encoding in Chinese (a Chinese 3 characters is too long), It is then passed to the server for shortening the character and encoding it with iso8859-1. The Doget method of the server should be iso8859-1 decoded before utf-8 decoding to see Chinese.
The POST request then stores the data in utf-8 form in the browser to the HTTP request body, without passing the URL, and then as long as request.setcharacterencoding ("Utf-8"); Notify request to decode in utf-8 form. , because request is decoded by default in Iso8859-1.
The first we can set the encoding format for the method (this setting must be before writer, otherwise invalid)
Response.setcharacterencoding ("Utf-8");
Response.setcontenttype ("Text/html;charset=utf-8");
Or
Content=new String (content.getbytes ("iso-8859-1"), "UTF-8");
The second kind we can write our own filter (recommended program)
private string charset;public void init (FilterConfig config) throws servletexception {system.out.println ("init");// gets the < in the Web. xml file init-param>// <param-name>encode</param-name>// <param-value>gbk</ Param-value>// </init-param> name for encode value for gbkcharset = Config.getinitparameter ("Encode");} Public void dofilter (Servletrequest request, servletresponse response,filterchain chain) throws ioexception, servletexception {request.setcharacterencoding (charset); Response.setcharacterencoding (CharSet); System.out.println (charset); Chain.dofilter (request, response);} Public void destroy () {system.out.println ("Destroy");}
Web.xml<filter><filter-name>encodingfilter</filter-name><filter-class> Com.sjs.filter.encodingfilter</filter-class><init-param><param-name>encode</param-name ><param-value>UTF-8</param-value></init-param></filter><filter-mapping>< Filter-name>encodingfilter</filter-name><url-pattern>/*</url-pattern>/filter-mapping>
The third Way we directly modify Tomcat's Server.xml
<connector port= "8080" protocol= "http/1.1"
connectiontimeout= "20000"
Redirectport= "8443" uriencoding= "UTF-8"/>
The above three kinds of basically can solve the problem,
But we also have a problem, is the database (MYSQL) settings, be sure to remember the installation Wizard when you must Character set to UTF8 the time must be selected (today I encountered the problem is this),
650) this.width=650; "height=" 293 "alt=" MySQL graphic installation tutorial "src=" http://files.jb51.net/file_images/article/201006/1_ 100612113212_1.jpg "width=" 389 "border=" 0 "style=" white-space:normal;border:1px solid RGB (204,204,204);p adding:3px; Font-family:tahoma, Helvetica, Arial, ' song Body ', Sans-serif;font-size:14px;line-height:25px;text-align:center; Background-color:rgb (247,252,255); "/>
Here's the wrong set of your MySQL tool inside is to view the database code is not visible, only through show variables like "%char%"; Command View settings
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4C/F1/wKioL1RH4LLT97zPAAHjnyg8d5k447.jpg "title=" 222. PNG "alt=" wkiol1rh4llt97zpaahjnyg8d5k447.jpg "/>
Red box is the wizard set the encoding, not set before latin1, if you want to modify this value can only be run mysql/bin/mysqlinstanceconfig.exe, reset
Do not use commands ( set @ @character_set_server = ' UTF8 ';) After the service restarts, it is invalid! This place is not very well designed, it will be restarted after changing this value, and this setting is invalid after restarting.
This article is from the "Silent Years" blog, please be sure to keep this source http://6229839.blog.51cto.com/6219839/1566969
JAVA common garbled problem and MySQL character_set_server setup