Chinese garbled classification:
(1) According to the request classification:
GET request garbled
Post request garbled
(2) According to garbled location classification
Data from the foreground to the background garbled (data stored in the database garbled)
From the background to the foreground data garbled (displayed in the page garbled data)
Garbled way to solve:
(1) Get request parameter garbled (note: Get request garbled, spring Configuration and encoding interceptor configuration is invalid)
method One: processing the encoding in the background, using the GetBytes method to convert the encoding mode
String username = request.getParameter("username"); //获取请求参数值
username = new String(username.getBytes("iso8859-1"),"UTF-8");//将请求参数值转换为UTF-8编码格式
method Two: in the client using Urlencoder.encode (name, "UTF-8") to encode the Chinese parameters, on the server side need to decode Naem = Java.net.URLDecoder.decode (Name, " UTF-8 "));
/* Use hyperlink jump in JSP page: Respond to delete Note comments */
function Deletenotecomment (name) {
Window.location.href= "<%=request.getcontextpath ()%>/note/deletecomment.do?name=" + Urlencoder.encode (name, "UTF-8");
}
Receive request parameters in Spring's controller class
String name = Request.getparameter ("name"); //Get request Parameters
Name = Urldecoder.decode (name, "UTF-8")); //Convert request parameters to UTF-8 encoded format
Method Three: modify Tomcat's Server.xml file:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
(2) Post request Chinese garbled solution method
method One: by setting the encoding of the request parameter
Add a statement in the first sentence of the method: request.setcharacterencoding ("UTF-8");
Add a statement to the last sentence of the method: response.setcontenttype ("Text/html;charset=utf-8");
method Two: write the coding interceptor
Interceptor class Characterencodingfilter. Java
PackageCn.dfx.share_record_SSM.dsum.util;Importjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classCharacterencodingfilterImplementsfilter{PrivateString encoding=NULL; Public voiddestroy () {} Public voidDoFilter (servletrequest req, Servletresponse resp, filterchain chain)throwsIOException, servletexception {httpservletrequest request=(httpservletrequest) req; HttpServletResponse Response=(HttpServletResponse) resp; request.setcharacterencoding (encoding); Chain.dofilter (Request,response); Response.setcontenttype ("Text/html;charset=utf-8"); } //get initialization parameters in the configuration file Public voidInit (filterconfig config)throwsservletexception {encoding= Config.getinitparameter ("Encoding"); }}
The interceptor configuration in Web. xml
<filter> <filter-name>character</filter-name> <filter-class> cn.dfx.share_record_ssm.dsum.util.characterencodingfilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </ init-param> </filter> <filter-mapping> <filter-name>character</ filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
method Three: Add the configuration in the Web. xml file directly using the Spring encoding interceptor
<filter> <filter-name>encodingFilter</filter-name> <filter-class> Org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> < Param-name>forceencoding</param-name> <param-value>true</param-value> </init-param> </filter>
<filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern> /* </url-pattern> </filter-mapping>
method Four: requestmapping configuration in the Spring controller class
@RequestMapping (value= "/gomain", produces= "Application/json;charset=utf-8")
Or: @RequestMapping (value= "/gomain", produces= "Text/html;charset=utf-8")
Method Five: Add the following configuration in spring's master profile (this is the trick!!!) ) Spring-servlet.xml
<!--handling Spring's controller class to return JSON data in Chinese garbled configuration-- class = " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "> <property name=" Messageconverters "> <array> class =" Org.springframework.http.converter.StringHttpMessageConverter "> <property name=" Supportedmediatypes " Value = "Text/plain;charset=utf-8"/> </bean> </array> </property> </bean>
Java Solutions for garbled Chinese