1

I'm trying to save Chinese characters from a form submit into the database.

I've set the contentType on the jsp via

<%@ page contentType="text/html;charset=UTF-8" %>

I've also set this tag inside the of the jsp:

<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8" />

However, when I submit the form, my controller sees a different character than what I entered.

I am entering the character 我 and seeing æ?? in the controller. When the data redisplays on the page, it shows the same wrong character (æ??).

Why isn't the controller getting the correct character?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Corey
  • 1,133
  • 4
  • 17
  • 30

3 Answers3

3

Declare a CharacterEncodingFilter in your web.xml file before any other filter

<filter>
    <filter-name>charsetFilter</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>
</filter>

<filter-mapping>
    <filter-name>charsetFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

In your jsp file try adding this at the very start of the file:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
Enrique
  • 9,920
  • 7
  • 47
  • 59
  • The `language` and `contentType` parts are by the way superfluous here. It are already the defaults. Just `<%@ page pageEncoding="UTF-8" %>` is sufficient. – BalusC Dec 01 '10 at 04:16
  • This will convert data to ?(question mark) only, i am using sring, hibernate and mysql. – Yogesh Prajapati Aug 04 '15 at 15:54
1

Not all browsers will respect the character set you've specified in the page or the form. Spring provides a filter, the CharacterEncodingFilter, that can add a character encoding or force a particular encoding, as the request comes in and before it hits the controller.

Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126
  • I've set the filter using Spiff's response in http://forum.springsource.org/showthread.php?t=14063. – Corey Nov 30 '10 at 21:32
0

Add an accept-charset attribute to your form:

<form method="POST" accept-charset="utf-8" ... >

This tells the browser to submit your form contents as UTF-8 to the server.

Jan Thomä
  • 13,296
  • 6
  • 55
  • 83
  • This attribute is marked optional by HTML spec and ignored by practically all browsers when the charset is present in the response header which is then preferred. Don't use it. – BalusC Dec 01 '10 at 04:14