tags:

views:

176

answers:

1

Hi,

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"&gt;
<% 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>

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?

+3  A: 

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" %>

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. 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.

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}
BalusC