我们有一个 servlet 程序,它在 servlet 程序中使用 HTML 代码,即
HTML.append("<html>");
HTML.append(lnTag);
HTML.append("<head>");
HTML.append(lnTag);
HTML.append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
String titalsLang = resourceBundle.getString("eayslip.tan.title");
HTML.append("<title>"+resourceBundle.getString("eayslip.tan.title")+"</title>");</i>
// and list of codes...
out.print(HTML);
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
我在调试代码时从属性文件中获取中文字符。但是一旦响应被发送到页面,在 html 页面中我们就会得到问号??????。
我们没有遇到英文字符的问题,它们显示正确。
最佳答案
我怀疑问题是您正在设置内容类型 - 包括编码 - 在调用 HttpServletResponse.getWriter()
之后,假设那是 out
来自.
来自 ServletResponse.setContentType
的文档:
This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.
基本上,您应该在调用 getWriter
之前设置响应中的所有 header ...如果您调用的是 getOutputStream
而不是 getWriter
,你不应该......对文本数据使用编写器,对二进制数据使用流。
关于java - 在 HTML 文件中显示汉字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32516420/