1

I'm using Google App Engine to retrieve data from database. when I retrieve arabic texts, it returns something like that "?§?? ?¹????????"

here is 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("السلام عليكم:");
}
}

the output is:

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>

I'm using: Android Studio - Runtime: JAVA 7 - Environment: Standard

this is from My Browser Chrome: Response and Request Headers

0

1 Answer 1

0

I found the solution.

the problem is that I get the writer before setting content type

so the code should be:

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("السلام عليكم:");
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.