JSP page form submission, controller received garbled, database garbled and other solutions

Source: Internet
Author: User

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”%>
    • 1

Add in Head

<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8”>
    • 1

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"); 
    • 1
    • 2

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"/>
    • 1
    • 2

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

Related Article

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.