简体   繁体   中英

Java - Google App Engine - Encoding problems with UTF-8

I would like to add some characters with accents in my ".jsp" and ".html" but when I write those characters with accent in those files and load my pages:

  • "è" becomes "é"
  • "ç" becomes "ç"
  • "é" becomes "Ã "

Steps:

  1. I have configured Eclipse for creating all files with UTF-8 encoding (I have checked: all .jsp and .html files in the /src or /target folder are now UTF-8).

  2. None of my .jsp or .html files (inside /src or /target folder) explicitely contains those "é", "ç", "à ".

  3. All my .jsp contains those lines at the beginning :

     <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta content="IE=edge" http-equiv="X-UA-Compatible" /> <meta content="width=device-width, initial-scale=1" name="viewport" /> <!-- ... --> </head> 
  4. I have added those parameters in my appengine-web.xml:

     <system-properties> <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/> <!-- Added below --> <property name="file.encoding" value="UTF-8" /> <property name="DEFAULT_ENCODING" value="UTF-8" /> </system-properties> 

I have also tried to add this thing showed in the GAE documentation but my application throws Exception :

<env-variables>
    <env-var name="DEFAULT_ENCODING" value="UTF-8" />
</env-variables>

GAE documentation says ( source ) :

To avoid conflicts with your local environment, the development server does not set environment variables based on this file, and requires that the local environment have these variables already set to matching values. (This does not apply to system properties.)

So I have removed this part and added the following environment variable in my system: DEFAULT_ENCODING = UTF-8

Further more:

  • When I use firebug to see the network, it shows Content-Type text/html; charset=utf-8 Content-Type text/html; charset=utf-8 for my loaded page.
  • Google's tool has already added the <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> in my pom.xml when I've generated my project.

Interesting fact:

  • When I load html code from my text files with AJAX, there is no problem, "é" or "ç" characters are ok. I use the BalusC's code ( and you can notice that he uses response.setCharacterEncoding("UTF-8"); so I don't know where the problem comes from).
  • On the development environment (my localhost), if I replace <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %> by this <%@ page pageEncoding="UTF-8" %> characters with accents are ok, but only on my localhost , then when I upload my project to Google App Engine servers it's still not ok , firebug doesn't show Content-Type text/html; charset=utf-8 Content-Type text/html; charset=utf-8 anymore, and W3C Markup Validation Service tells me :

    Internal encoding declaration utf-8 disagrees with the actual encoding of the document (windows-1252)

My configuration:

  • Maven 3.2
  • IDE: Eclipse Luna
  • OS: Windows 7

I ran into the same issue. My solution was to add at the top of my .jsp files that generate html the following tag: <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM