Solution:
Step 1. Add a filter, that is, in the filter in init () plus request.setcharacterencoding ("Utf-8"), the equivalent of each page encoding is utf-8,
Step 2. Change the form submission method to post mode, as far as the get way is solved
The above two steps ensure that the data in the process of submission to the background is not garbled
Step 3. The types of fields that need Chinese in the database are changed to nvarchar to ensure that the data is not garbled when it is written to the database.
Finally, the <%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>charset=utf-8 " Write utf-8 to ensure that the code is encoded as Utf-8 at the time of output
Some of the following online statements:
In fact, this is a very common problem, the Internet also has a lot of articles to comprehensively explain the JSP Chinese garbled problem. Here I just want to say a get way when submitting a form in Chinese garbled solution.
Now when doing the system, in order to solve the Chinese garbled problem, we usually configure a code filter, such as we directly with spring to provide us with the Code filter
<!--code Filter-->
<filter>
<filter-name>spring character encoding filter</filter-name>
<filter-class>
Org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
This configuration is equivalent to writing the following code in the code:
Request.setcharacterencoding ("Utf-8");
Response.setcharacterencoding ("Utf-8");
In addition, you can customize the filter
&NBSP;1 Create the Chinesefilter class. Package spell; 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.http.HttpServlet; Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletResponse; public class Chinesefilter extends HttpServlet implements Filter {private filterconfig filterconfig;//handle the passed- In filterconfig public void init (Filterconfig filterconfig) throws servletexception {this.filterconfig = Filterconfig;} Process the request/response pair public void Dofilter (ServletRequest request, servletresponse response, Filterchain fi Lterchain) {try {String encoding=filterconfig.getinitparameter ("encoding");//Remove parameters from Web.xml configuration file so that we can modify the encoding format by configuration. request.setcharacterencoding (encoding);//Set the encoding format of the request Filterchain.dofilter (request, response); catch (ServleTexception sx) {filterconfig.getservletcontext (). log (Sx.getmessage ());} catch (IOException Iox) { Filterconfig.getservletcontext (). log (Iox.getmessage ()); }//clean up resources public void Destroy () {} protected void doget (HttpServletRequest arg0, HttpServletResponse arg1) Throws Servletexception, IOException {//TODO auto-generated Method Stub super.doget (arg0, arg1);} protected void Dopos T (httpservletrequest arg0, HttpServletResponse arg1) throws Servletexception, IOException {//TODO auto-generated method Stub super.dopost (arg0, arg1); } 2 Web.xml Configuration (Note: jsp/servlet2.3 the above version to support filter, so the previous should be changed to <! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD ">" <filter> <filter-name>ChineseFilter</filter-name> <filter-class>spell. Chinesefilter</filter-class> <init-param> <param-name>encoding</param-name> < Param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>ChineseFilter</filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
In the JSP page, set the encoding for the page's storage encoding and page output:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "Utf-8"%>
In this way, the code is unified Utf-8.
This way there is no problem with having Chinese in the post submission form. But when submitted with GET, if it contains Chinese, it will appear similar to ... "The garbled question. The reason for this is that Tomcat has a different approach to get and post two ways of submitting. since tomcat5.x started, the Get and post methods submitted information, Tomcat used a different way to process the encoding, and for post requests, Tomcat still uses the encoding set by the Request.setcharacterencoding method to process, if not set, the Use the default iso-8859-1 encoding. The GET request is different, and Tomcat does not consider using the encoding set by the Request.setcharacterencoding method, but always uses the iso-8859-1 encoding.
The solution is as follows:
1. Configure Tomcat configuration file Server.xml in the sentence:
<connector uriencoding= "GB2312"
port= "8080" maxhttpheadersize= "8192"
maxthreads= "minsparethreads=" "25" Maxsparethreads= "$"
Enablelookups= "false" redirectport= "8443" acceptcount= "M"
connectiontimeout= "20000" disableuploadtimeout= "true"/>
Add the phrase: uriencoding= " GB2312 "
2. Use string Name=new string (Request.getparameter (" name "). GetBytes (" Iso-8859-1 ")," GB2312 "); Convert encoding
The second method is recommended for
.