我的 servlet 看起来像这样
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>");
}
我的浏览器可以显示其他网站的中文字符。 我正在尝试两种不同的方式来显示汉字,但它们都显示为??? 正确的做法是什么?
最佳答案
尚未为响应设置显式编码。因此,响应将由容器使用默认编码 ISO-8859-1 写入。
因此,您需要使用 HttpServletResponse.setCharacterEncoding() 指定适当的字符编码或HttpServletResponse.setContentType方法。这可以是:
response.setCharacterEncoding("GB18030");
response.setContentType("text/html; charset=GB18030");
您还可以使用 UTF-8 作为显式编码。
关于java - 如何从 Java servlet 生成汉字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6295496/