Google Appengine Cloud 不支持 JSP/Servlet 响应中的 unicode 字符吗?

发布于 2024-10-15 20:26:42 字数 346 浏览 1 评论 0原文

我正在向部署在 Google Appengine 服务器上的应用程序发出请求。应用程序返回 unicode 格式的响应。如果我通过开发服务器访问响应,它正如我预期的那样很好,但是当我部署到 Google 生产应用引擎服务器上时,这一切都出现了一个问号,如下所示 "header":"������������������������”

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

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

请帮忙。

I am making a request to my application deployed on to Google Appengine server. The application returns a response which is in unicode. The response if I access through development server, it comes nicely as I expected it, but when I deployed on to Google production appengine server, it all comes a question marks as following
"header":"������������������������"

if you look at the source of servlet, I ensured that following is already in place

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

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2024-10-22 20:26:42

我刚刚尝试从 JSP 页面返回 UTF-8 字符,它们似乎可以正确呈现。在 JSP 中,我有 并在 head 。未在 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>

I just tried returning UTF-8 characters from a JSP page and they seem to render correctly. In the JSP I have <jsp:directive.page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" /> and in head <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />. Not setting anything about content type or encoding to response in the Servlet.

I also tried retrieving and storing UTF-8 data to and from the datastore both locally and deployed and it renders all the UTF-8 characters properly on both occasions.

Here's the 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;
  }
}

And the 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>
相对绾红妆 2024-10-22 20:26:42

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

解决方案是添加

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

到jsp文件的顶部。

I was having this problem as well -- certain unicode characters in my jsp (such as →) rendered fine with the development server, but not when deployed in appengine. This even though the html head contained .

The solution was to add

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

to the top of the jsp file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文