JSP中UTF8编码的XHTML内容 [英] UTF8 encoded XHTML content in JSP

查看:84
本文介绍了JSP中UTF8编码的XHTML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JSP页面中显示XHTML内容.编码为UTF8. jsp页面在java类中调用一个方法,该方法返回xhtml内容.基本上这就是我的jsp页面中的内容

I am trying to display XHTML content in a JSP page. The encoding is UTF8. The jsp page calls a method in a java class and the method returns the xhtml content. Basically this is what I have in my jsp page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd">
<% response.setContentType("application/xhtml+xml"); %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
</head>
<body>
<% response.setContentType("application/xhtml+xml"); %>
<%=myJavaClass.getXHTML()%>
</body>
</html>

问题是某些字符显示为问号(?). 但是,如果Java类(myJavaClass)将相同的内容写入文件,则当我打开该文件并在文本编辑器中查看该文件时,所有字符都会正确显示. Tomcat的控制台还显示了所有正确字符的xhtml内容,只是在浏览器中我看到了问号.

The problem is that some characters show up as QuestionMarks (?). However, if the java class (myJavaClass) writes the same content to a file, all characters are displayed correctly when I open up the file and view it in a text editor. Tomcat's console also shows the xhtml content with all correct characters, only in the browser I am seeing question marks.

有人能想到发生这种情况的任何原因吗?

Can anyone think of any reason why this is happening?

推荐答案

有很多因素可以发挥作用.在您的特定情况下,您正在使用老式的 scriptlets 将XML字符串写入响应中. <%= foo %>隐式调用response.getWriter().write(foo).您还需要通过将以下内容添加到JSP的 top 中来设置响应编写器的字符编码:

There are lot of factors which can play a role. In your specific case you're using the old fashioned scriptlets to write the XML string to the response. The <%= foo %> implicitly calls response.getWriter().write(foo). You need to set the character encoding of the response writer as well by adding the following to top of your JSP:

<%@ page pageEncoding="UTF-8" %>

这将通过隐式调用response.setCharacterEncoding("UTF-8") 来将响应编码设置为UTF-8,并且还将添加相应的响应头(如果尚未完成的话).

This will set the response encoding to UTF-8 by implicitly calling response.setCharacterEncoding("UTF-8") and it will also add the appropriate response header if not done yet.

您真正需要考虑的所有因素是:

All factors which you really need to take into consideration are:

  1. 请求编码.对于GET请求,需要在appserver的配置中进行设置.对于POST请求,您需要使用HttpServletRequest#setCharacterEncoding().
  2. 响应编码.这已经在这里回答了.
  3. 数据库编码.在SQL CREATE中指定编码.
  1. Request encoding. For GET requests this needs to be set in appserver's configuration. For POST requests you need to use HttpServletRequest#setCharacterEncoding().
  2. Response encoding. This is already answered here.
  3. Database encoding. Specify the encoding during SQL CREATE.

有关更多背景信息和所有解决方案的详细概述,您可以找到这篇文章有用.

For more background information and an detailed overview of all solutions you may find this article useful.

也就是说,如果您已经在HTML头中设置了<meta http-equiv="content-type">,则行<% response.setContentType("application/xhtml+xml"); %>完全多余.摆脱它们,如果可能的话,也摆脱掉脚本.只需使用EL:

That said, the lines <% response.setContentType("application/xhtml+xml"); %> are completely superfluous if you already have set the <meta http-equiv="content-type"> in HTML head. Get rid of them and if possible also of the scriptlets. Just use EL:

${someBean.somePropertyReturningXmlString}

这篇关于JSP中UTF8编码的XHTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆