SPRINGMVC garbled problem

Source: Internet
Author: User

Add to Web. xml

<Filter>    <Filter-name>Characterencodingfilter</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>Characterencodingfilter</Filter-name>    <Url-pattern>/*</Url-pattern></filter-mapping>

Preferably placed in the first filter, equivalent to the execution of request.setcharacterencoding (this.encoding);

1. The file must be stored in one encoding; the read file must also be read in one encoding, if not specifically set, to the system default encoding, Chinese windows for GBK encoding.

2. the process from. Java->.class is that Write the. java file and save it as an encoding, and then compile the file with the Javac method, noting that the. Java is not saved by the system default encoding to indicate the actual encoding with the encoding parameter, otherwise an error occurs, and the resulting. class file is saved as the system default encoding.

3 . From. Jsp->.java->.class, Save as a. jsp file for some encoding, then Tomcat reads from the pageencoding and translates to the servlet as the system default encoding, and then the same as above. java- >.class process.

4.The IDE's encoding is the way to decode or save the file opened under the system. Exception: If the. jsp file has <%@ page language= "java" pageencoding= "UTF-8"%>, then eclipse is automatically saved as UTF-8 mode.

5.contenttype= "Text/html;charset=utf-8" means how the browser decodes the file when it is obtained

6.response.setcharacterencoding ("UTF-8") can <%@ page contenttype= "text/html;charset=iso8859-1"% > For Charset=utf-8, is to tell the browser how I encode this file.

7. form submission: Regardless of the form submission can be in the background of the Java file through string des = new String (S.getbytes ("iso8859-1"), "UTF-8"), to convert to the UTF-8 encoding you want. But if each place adds the words too troublesome, therefore divides the post and the get two kinds of ways to differentiate commits (TOMCAT5 after the separation processing, before the processing way, namely can use Request.setcharacterencoding ("UTF-8") method processing, However, TOMCAT5 after the Get commit method is not valid with this statement).

8.get

A, if you enter Chinese characters directly from the address bar, it is generally encoded as "GBK" and requires
New String (Request.getparameter ("something"). GetBytes ("Iso-8859-1"), "GBK") out

b, if it is a page hyperlink connection in the Chinese characters, the encoding according to the different page encoding, if the page
Content= "text/html; Charset=utf-8 ", in the configuration file in Tomcat/conf/server.xml:
<!--Define a non-ssl Coyote http/1.1 Connector on port 8080-
<connector port= "8080"
maxthreads= "minsparethreads=" maxsparethreads= "75"
Enablelookups= "false" redirectport= "8443" acceptcount= "100"
debug= "0" connectiontimeout= "20000" usebodyencodingforuri= "true"
Disableuploadtimeout= "true"/>

Plus: usebodyencodingforuri= "true" to properly use GetParameter to remove the correct content.
If content= "text/html; CHARSET=GBK ", requires
New String (Request.getparameter ("something"). GetBytes ("Iso-8859-1"), "GBK")
Take out, other things are similar.

Summarize:
1, all pages use UTF8 encoding,
2, server plus filter,
Do not use in 3,server.xml
<valve classname= "Org.apache.catalina.valves.RequestDumperValve"/>
4, find port= "8080" connector tag in Tomcat's conf\server.xml, plus usebodyencodingforuri= "true"
This should be able to deal with most of the Chinese problem at the front desk. As for the address bar input Chinese, not supported, general procedures rarely require
Enter it from here.

Six, connect the database

1. mysql configuration file:
Modify MySQL in Windows\my.ini default-character-set=utf-8

2, MySQL database and table are also set to Utf8_unicode_ci

3. Database Link: jdbc:mysql://localhost/mydb?useunicode=true&characterencoding=utf-8

For a Web container, if you don't set it, the default is Iso8859-1
String des = new String (S.getbytes ("iso8859-1"), "UTF-8"); You can use this wherever it is, garbled is used

SPRINGMVC garbled problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.