为什么 JSP 页面编码指令应该是 JSP 中的第一行?

发布于 2024-10-18 17:42:58 字数 228 浏览 0 评论 0原文

我的意思是下面的指令

<%@ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏璃陌 2024-10-25 17:42:58

问题是,该指令之前的任何内容都将其输出写入到浏览器的输出流中 - 并且编码指定该流如何将页面的字符串内容转换为字节,因此必须在以下情况下设置它:流是在向其中写入任何内容之前创建的。

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.

醉生梦死 2024-10-25 17:42:58

否则,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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文