Frank
Frank

Reputation: 31090

How to generate Chinese characters from a Java servlet?

My servlet looks like this

  protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
  {
    PrintWriter out=response.getWriter();

    out.println("<Html><Head><Title>Signup</Title></Head>\n<Body>\n");
    out.println("\u5982 电话\n");
    out.println("</Body>\n</Html>");
  }

My browser can display Chinese characters from other websites. I'm trying 2 different ways to display Chinese characters, but they all showed up as ??? What's the correct way to do it ?

Upvotes: 0

Views: 2323

Answers (4)

Hasan Sawan
Hasan Sawan

Reputation: 421

Just setting the character encoding as UTF-8 worked for me.

response.setCharacterEncoding("UTF-8")

Upvotes: 0

duffymo
duffymo

Reputation: 309008

You would need to send Unicode, have your servlet send UTF-8, and have the browser locale set up properly to interpret the characters correctly.

Upvotes: 0

Vineet Reynolds
Vineet Reynolds

Reputation: 76719

No explicit encoding has been set for the response. The response would therefore be written by the container with the default encoding of ISO-8859-1.

You'll therefore need to specify the appropriate character encoding using the HttpServletResponse.setCharacterEncoding() or HttpServletResponse.setContentType methods. This would be either of:

response.setCharacterEncoding("GB18030");
response.setContentType("text/html; charset=GB18030");

You may also use UTF-8 as the explicit encoding.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240996

Try adding

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 

Upvotes: 0

Related Questions