如何在Google应用引擎中设置Unicode?

时间:2016-01-15 08:52:15

标签: java google-app-engine google-cloud-datastore

我正在Google App Engine中构建我的应用

好的,我来了Servlet

public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
     String firstName=req.getParameter("firstName");
     System.out.println(URLDecoder.decode(firstName, "UTF-8")+" "+ firstName);
  }

现在,我发送了#34;Thủy"到Servlet并显示此输出

th?y th?y

然后,我设置了appengine-web.xml

<appengine-web-app>.......
 <env-variables>
    <env-var name="DEFAULT_ENCODING" value="UTF-8" />
  </env-variables>

</appengine-web-app>

然后在跑步时我遇到了这个错误:

com.google.appengine.tools.development.EnvironmentVariableChecker$IncorrectEnvironmentVariableException: One or more environment variables have been configured in appengine-web.xml that have missing or different values in your local environment. We recommend you use system properties instead, but if you are interacting with legacy code that requires specific environment variables to have specific values, please set these environment variables in your environment before running.
[Mismatch environmentVariableName=DEFAULT_ENCODING environmentVariableValue=null appEngineWebXmlValue=UTF-8 appEngineWebXmlFile=C:\Users\henry\workspace8\FluentEnglish\war\WEB-INF\appengine-web.xml]
    at com.google.appengine.tools.development.EnvironmentVariableChecker.check(EnvironmentVariableChecker.java:75)
    at com.google.appengine.tools.development.ApplicationConfigurationManager.checkEnvironmentVariables(ApplicationConfigurationManager.java:240)
    at com.google.appengine.tools.development.ApplicationConfigurationManager.access$000(ApplicationConfigurationManager.java:32)
    at com.google.appengine.tools.development.ApplicationConfigurationManager$WarModuleConfigurationHandle.checkEnvironmentVariables(ApplicationConfigurationManager.java:394)
    at com.google.appengine.tools.development.JettyContainerService.connectContainer(JettyContainerService.java:211)
    at com.google.appengine.tools.development.AbstractContainerService.createConnection(AbstractContainerService.java:273)
    at com.google.appengine.tools.development.AbstractInstanceHolder.createConnection(AbstractInstanceHolder.java:37)
    at com.google.appengine.tools.development.AbstractModule.createConnection(AbstractModule.java:73)

那么,出了什么问题?

为什么HttpServletRequest不显示Unicode文本?

如何让HttpServletRequest显示Unicode文本?

1 个答案:

答案 0 :(得分:1)

最后一条错误消息是由appengine-web.xml配置引起的,有关详情,请参阅this thread

您没有指定执行POST的方式,但以下示例使用您的处理程序成功(没有DEFAULT_ENCODING appengine-web配置):

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<form method="post" action="/api/test">
  <input type="text" name="firstName" value="Thủy"><br>
  <input type="submit"  name="OK">
</form>

虽然失败(发件人缺少内容类型):

<form method="post" action="/api/test">
  <input type="text" name="firstName" value="Thủy"><br>
  <input type="submit"  name="OK">
</form>