2

I'm having trouble with UTF-8.

common.jsp

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

typical.jsp

<%@ include file="common.jsp" %>

Page Head

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

Form

<form id="screenObject" accept-charset="UTF-8" action="/SiteAdmin/articleHeaderEdit?articleId=15" method="post">

I enter non latin1 characters into a text field and click Save. Validator complains about another field and stops the submission. This never gets to the database, so database ability to handle UTF-8 is not in this picture. The page redisplays with appropriate error but the text that had been entered is all messed up. All non latin1 characters are converted to some gibberish.

I'm using Spring 3 MVC, in case that matters...

Attempts

Adding this to my view resolver didn't help:

<property name="contentType" value="text/html;charset=UTF-8" />

Solution

Add encoding filter to web.xml.

<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
jacekn
  • 1,521
  • 5
  • 29
  • 50

1 Answers1

5

Someplace in the data flow you are using something other than UTF-8 to either encode a string as bytes or to decode a byte stream as a string. This typically happens when you use some API call that uses the default character encoding. Without knowing your code, that's all we can tell you.

Also see this link for character encoding problems in Spring, or just search the forum for [spring] character encoding for lots of other postings.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • After adding encodingFilter to my web.xml, Tomcat fails to start. WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SiteAdmin' did not find a matching property. INFO: Starting Servlet Engine: Apache Tomcat/7.0.12 java.lang.NullPointerException – jacekn Apr 15 '12 at 17:14
  • Do I have to save my files with UTF-8 encoding in Eclipse? – jacekn Apr 15 '12 at 17:15
  • Adding encoding filter fixes the problem described in my post. We need to be careful, however, as the link in your response points to a misspelt `filter-class` example. Copy and paste of that code results in an issue that is not immediately clear. – jacekn Apr 15 '12 at 17:43
  • @jacekn - I'm glad your problem's fixed. Good to know about the misspelling, too. Regarding Eclipse - you need to set the project (or workspace) encoding to UTF-8 only if you are putting UTF-8 characters in source files. I have a habit of setting the default encoding of all my workspaces to UTF-8. – Ted Hopp Apr 15 '12 at 18:15