1.web Project garbled problem
When doing Web projects, there will be more or less the problem of Chinese garbled.
For JSP page form submission, controller received garbled, save to database Chinese garbled and other problems, unified to give a few garbled solution.
1. The project code is not set to UTF8
There are some common garbled problems because we do not set the encoding of the project when we create a Web project or import someone else's Web project. Item not set UTF-8 encoding can cause Chinese garbled.
2. Ensure that the database is encoded as UTF8, MySQL, for example, can be changed to MySQL directory, My.ini file, restart the MySQL service
To restart the MySQL service method:
3. When the database is built, verify that the code is utf-8 to use Navicat as an example
4. Garbled problem in JSP page
Can be added at the top of the JSP;
<%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8”%>
Add in Head
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8”>
Form form is submitted as "Post"
5. If you are using spring, you can include it in the project's 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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
6. In the controller layer of the Java code or in the action submitted by the form form, set:
request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");
7.tomcat settings
Open the Conf-server.xml file under the Tomcat installation path:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
The above 7 kinds are basically used to solve the JSP page form submission, controller received garbled, database garbled and other problems, the general 4,5,6 method is the most commonly used is the largest relationship.
For example, I have to write form form, submit data to the database, open the database found Chinese garbled, set what Utf-8 and the like are set.
In the 5th way, a character filter is added.
Web. xml:
Effect:
JSP page form submission, controller received garbled, database garbled and other solutions