Jsp页面中显示字符编码的问题
我编写了jsp页面,在其中创建了带有gb2312字符集的字符串,然后将字符串传递给JSP页面。但在jsp页面中,我将页面编码和元标记指定为UTF-8。我希望这些字符必须显示为垃圾字符,并且在我将浏览器中的字符编码更改为 gb2312 后,它必须正确显示。但实际发生的情况是 UTf-8 本身显示为正确的。但是当我将其更改为 gb2312 字符编码时,它已显示为垃圾编码。请帮忙。我做错了什么。帮忙纠正一下。
I have written jsp page in which i create String with gb2312 charset, Then i pass the string to the JSP Page. But in the jsp page i have give pageencoding and meta tag as UTF-8. I am expecting that the characters have to be displayed as junk one and after i change character encoding in browser as gb2312 it has to be displayed correctly. But what actually happen was in UTf-8 itself it displayed as correct one. But when i change that to gb2312 Character encoding it has been displayed as junk one. Please Help. What am i doing wrong. Help to correct me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符串没有编码。它只是一个字符数组,每个字符都有一个通用的 unicode 值。
仅当必须将字符转换为字节以保存在文件中或流式传输到浏览器时,才使用编码将字符转换为字节。
由于您在 JSP 页面编码指令中表示必须使用 UTF-8,因此您的字符串已被编码为 UTF-8。由于响应标头,浏览器知道它是 UTF-8,因此将从服务器接收到的字节正确转换为字符。
如果您告诉浏览器忽略作为响应标头的编码集,并使用 gb2312,则浏览器将尝试将字节解释为 gb2312,并且由于它是 UTF-8,因此将显示不正确的字符。
A String doesn't have an encoding. It is just an array of chars, and each char has a universal unicode value.
It's only when the chars must be transformed into bytes, to be saved in a file or streamed to a browser, that the encoding is used to transform chars into bytes.
Since you said in your JSP's page encoding directive that it had to use UTF-8, your string has been encoded as UTF-8. The browser knows that it's UTF-8 thanks to a response header, and thus transforms the bytes it receives from the server into chars correctly.
If you tell the browser to ignore the encoding set as a response header, and use gb2312, the browser will try to interpret the bytes as gb2312, and since it's UTF-8, it will display incorrect chars.