Java Servlet Google App Engine中的编码[重复]

问题描述 投票:1回答:1

这个问题已经在这里有了答案:

我正在使用Google App Engine从数据库检索数据。 当我检索阿拉伯语文本时,它返回类似“?§???¹¹?????????”的内容

这是HttpServlet:

public class MyServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    PrintWriter writer = response.getWriter();

    final String contentType = "text/plain;charset=UTF-8";
    final String characterEncoding = "UTF-8";

    response.setContentType(contentType);
    response.setCharacterEncoding(characterEncoding);

    writer.println(response.getContentType());
    writer.println(response.getCharacterEncoding());
    writer.println("السلام عليكم:");
}
}

输出为:

text/plain; charset=iso-8859-1
ISO-8859-1
?§???³???§?? ?¹????????:

appengine-web.xml:

<?xml version="1.0" encoding="utf-8"?>

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

    <application>myApplicationID</application>

    <use-google-connector-j>true</use-google-connector-j>

    <version>1</version>

    <threadsafe>true</threadsafe>

    <system-properties>

         <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />

         <property name="file.encoding" value="UTF-8" />

         <property name="appengine.file.encoding" value="UTF-8" />

         <property name="DEFAULT_ENCODING" value="UTF-8" />

    </system-properties>

    <env-variables>

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

    </env-variables>
</appengine-web-app>

我正在使用:Android Studio-运行时:JAVA 7-环境:标准

这是来自“我的浏览器Chrome”: 响应和请求标头

java google-app-engine servlets character-encoding
1个回答
0
投票

我找到了解决方案。

问题是我在设置内容类型之前先获得作家

因此代码应为:

public class MyServlet extends HttpServlet {
   @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws IOException {
       final String contentType = "text/plain;charset=UTF-8";
       final String characterEncoding = "UTF-8";

       response.setContentType(contentType);
       response.setCharacterEncoding(characterEncoding);

       PrintWriter writer = response.getWriter();

       writer.println(response.getContentType());
       writer.println(response.getCharacterEncoding());
       writer.println("السلام عليكم:");
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.