当我练习Java Servlet全球化时,我遇到了以下问题:
NumberFormat.getCurrencyInstance(Locale.UK)
正在完美地工作
NumberFormat.getCurrencyInstance(Locale.JAPAN)
显示'?'而不是'¥'。
这是我的代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Detecting Locale </title>");
out.println("</head>");
out.println("<body>");
long number = 5_000_000L;
NumberFormat numForUK = NumberFormat.getCurrencyInstance(Locale.UK);
out.println("<p>Format currency with UK locale: " + numForUK.format(number) + "</p>");
NumberFormat numForJAPAN = NumberFormat.getCurrencyInstance(Locale.JAPAN);
out.println("<p>Currency Format using Japan Locale: " + numForJAPAN.format(number));
out.println("</body>");
out.println("</html>");
}
Google Chrome上的输出:
使用英国语言环境格式化货币:£5,000,000.00
使用日本区域设置的货币格式:?5,000,000
请帮我解决问题。 谢谢!
答案 0 :(得分:2)
尝试代替response.setContentType(&#34; text / html&#34;); response.setContentType(&#34; text / html; charset = utf-8&#34;);