为什么 JSP 页面编码指令应该是 JSP 中的第一行?
我的意思是下面的指令
<%@ page contentType="text/html; charset=UTF-8" %>
我有很多 JSP。我将这一行写在一个公共文件中,该文件已包含在所有 JSP 中。但这没有帮助,我可以看到浏览器使用与我在上面指令中指定的编码不同的编码。
我必须手动编写该指令作为第一行来解决问题。
我在
I mean the following directive
<%@ page contentType="text/html; charset=UTF-8" %>
I have lots of JSPs. I wrote this line in a common file that was already included in all JSPs. But that didn't help, I could see browser using different encoding than what I specified in above directive.
I had to manually wrote that directive as the first line to solve the problem.
I am asking this question in context of problem raised in this article
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,该指令之前的任何内容都将其输出写入到浏览器的输出流中 - 并且编码指定该流如何将页面的字符串内容转换为字节,因此必须在以下情况下设置它:流是在向其中写入任何内容之前创建的。
The problem is that anything that comes before that directive will have its output to be written to the output stream that goes towards the browser - and the encoding specifies how that stream translates the String content of the page to bytes, so it must be set when the stream is created, before anything is written to it.
否则,Java 运行时如何知道该文件是 JSP,从而对其进行编译和处理,而不是作为纯文本推送到客户端?
how else would the Java runtime know the file is a JSP and thus to compile and handle it as such, rather than as plain text to be just pushed to the client?