我正在 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);
}
现在,我将“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 文本?
最佳答案
最后一条错误消息是由 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>
关于java - 如何在 Google 应用引擎中设置 Unicode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34807330/