【问题标题】:Doesn't Google Appengine Cloud support unicode characters in response from JSP/Servlets?Google Appengine Cloud 不支持响应 JSP/Servlet 的 unicode 字符吗?
【发布时间】:2011-02-03 12:30:09
【问题描述】:

我正在向部署在 Google Appengine 服务器上的应用程序发出请求。应用程序返回 Unicode 格式的响应。如果我通过开发服务器访问的响应,正如我所期望的那样,但是当我部署到谷歌生产应用程序引擎服务器时,这一切都是一个问号,如下所示 “标题”:“������������������������”

如果您查看 servlet 的源代码,我确保以下内容已经到位

response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

请帮忙。

【问题讨论】:

  • HTTP 响应头本身包含什么? (萤火虫:Net > HTML)。浏览器说什么? (Firefox:查看 > 字符编码
  • 以下是原始响应:HTTP/1.1 200 OK Content-Type: text/html; charset=utf-8 内容编码:gzip 日期:2011 年 2 月 3 日星期四 11:59:55 GMT 服务器:Google 前端缓存控制:私有,x-gzip-ok="" Content-Length: 288
  • 问题文本是直接来自 JSP 还是直接来自 java 代码,还是间接来自表单输入?如果它来自表单输入,我之前已经看到过,并且问题可能在处理库中很深 - 尝试将表单切换到多部分编码。
  • 此响应直接来自 Servlet。我实际上已经在数据存储区中存储了一些数据。我正在通过 servlet 获取这些数据。
  • 那么在 GWT 上传输/部署后,包含这些字符的源文件本身就会损坏,或者使用错误的编码加载/读取它们。由于我不使用 GWT,因此无法给出更具体的解决方法。

标签: google-app-engine jsp servlets


【解决方案1】:

我刚刚尝试从 JSP 页面返回 UTF-8 字符,它们似乎可以正确呈现。在 JSP 中,我有 <jsp:directive.page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" /> 和头部 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />。没有在 Servlet 中设置任何关于内容类型或编码的响应。

我还尝试在本地和部署的数据存储中检索和存储 UTF-8 数据,它在两种情况下都能正确呈现所有 UTF-8 字符。

这是 Servlet:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    datastoreService.put(new Example((String) req.getParameter("field"))
            .getEntity());
    resp.sendRedirect("/");
}

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsp/list.jsp");
    final Iterator<Example> examples = Iterators.transform(
        datastoreService.prepare(new Query(Example.class.getSimpleName())).asIterator(),
        new Function<Entity, Example>() {
          @Override
          public Example apply(final Entity input) {
            return new Example(input);
          }
        });
  req.setAttribute("examples", examples);
  requestDispatcher.forward(req, resp);
}

public static class Example {
  private final Entity entity;

  public Example(final String field) {
    entity = new Entity(Example.class.getSimpleName());
    entity.setProperty("field", field);
  }

  public Example(Entity entity) {
    this.entity = entity;
  }

  public String getField() {
    return (String) entity.getProperty("field");
  }

  public Entity getEntity() {
    return entity;
  }
}

还有 JSP:

<jsp:directive.page contentType="text/html;charset=UTF-8"
  language="java" isELIgnored="false" />
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  version="-//W3C//DTD XHTML 1.1//EN" xml:lang="en" xmlns:og="http://opengraphprotocol.org/schema/">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>Examples</title>
  </head>
  <body>
    <h1>Examples</h1>
    <form action="/" method="post">
      <div>
        <input type="text" name="field" />
        <input type="submit" value="Submit" />
      </div>
    </form>
    <ul>
<c:forEach var="example" items="${examples}">
      <li>
        <p><c:out value="${example.field}" /></p>
      </li>
</c:forEach>
    </ul>
  </body>
</html>

【讨论】:

  • 您好,感谢您的回复。所以当我输入 out.println("some unicode text") 它确实有效。当我在数据存储中存储一些 unicode 数据并且直接存储 unicode 数据时它可以工作,但是当我存储从网站获取的 unicode 数据并将其存储在数据存储中时,我看不到它以 Unicode 存储。即使我将该字符串读取为具有 unicode 编码的字节,它也不起作用,它仍然不会将其存储为 unicode 数据
  • 上述问题仅存在于生产appengine google服务器。但在本地机器上,它按我的预期工作。
  • 我尝试在数据存储中存储和检索数据,并且 UTF-8 字符正确显示。请参阅上面的更新答案。
【解决方案2】:

我也遇到了这个问题——我的 jsp 中的某些 unicode 字符(例如 →)在开发服务器上渲染得很好,但在 appengine 中部署时却没有。即使 html 头包含 .

解决方案是添加

    <%@page pageEncoding="UTF-8"%>

到 jsp 文件的顶部。

【讨论】:

    猜你喜欢
    • 2018-06-14
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 2012-08-05
    • 2023-04-10
    相关资源
    最近更新 更多