当 HTML 由 servlet 调度时,显示的代码无法识别
我编写了一个登录页面,当您失败时,该页面将由 servlet 分派回来并带有提示。当 HTML 代码为“UTF-8”时,会显示无法识别的代码。我把代码改成“GB2312”后就正常了。为什么?
I write a login page, when you fail,the page will be dispatchered back with a tip by a servlet. When the HTML Code is "UTF-8", It's displayed with unrecognizable code. After I changed the code by "GB2312", It's normal. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的意思是您看到的是Mojibake?您需要确保在任何地方都使用 UTF-8。
您的编辑器必须将 JSP 文件保存为 UTF-8。如何做到这一点取决于编辑器。例如,在 Eclipse 中,它可以通过 Window > 进行配置。首选项>网页> JSP 文件>编码。
您的 JSP 必须将响应编码设置为 UTF-8。其中一种方法是将其放在 JSP 的顶部:
这还将使用 UTF-8 字符集隐式设置 HTTP 响应
Content-Type
标头,以便网络浏览器了解必须使用什么字符集来呈现页面。这还将隐式告诉浏览器必须使用什么字符集来发送提交的 POST 表单数据。您的服务器必须将请求 URI 解释为 UTF-8,以便正确解码 GET 参数。如何做到这一点取决于服务器。例如,对于 Tomcat,只需将
URIEncoding="UTF-8"
添加到/conf/server.xml 中的
.
元素即可您的 Web 应用程序必须将请求正文解释为 UTF-8,以便正确解码 POST 参数。常见的做法是在
Filter
中执行此操作。另请参阅:
You mean that you're seeing Mojibake? You need to ensure that you're using UTF-8 everywhere.
Your editor must save JSP files as UTF-8. How to do it depends on the editor. In Eclipse for example, it's configureable by Window > Preferences > Web > JSP files > Encoding.
Your JSP must set the response encoding to UTF-8. One of the ways is to put this in top of the JSP:
This will also implicitly set the HTTP response
Content-Type
header with the UTF-8 charset so that the webbrowser understands what charset it has to use to present the page. This will also implicitly tell the browser what charset it has to use to send the submitted POST form data.Your server must interpret request URI as UTF-8 so that GET parameters are properly decoded. How to do it depends on the server. In case of for example Tomcat, it's a matter of adding
URIEncoding="UTF-8"
to the<Connector>
element in/conf/server.xml
.Your webapp must interpret request body as UTF-8 so that POST parameters are properly decoded. A common practice is to do it in a
Filter
.See also: