Reputation: 1571
I have a JSP page that displays XHTML content. I have included the following lines at the top of my JSP page:
<?xml version="1.0" encoding="UTF-8"?>
<% response.setContentType("application/xhtml+xml"); %>
<% request.setCharacterEncoding("UTF-8"); %>
If I change the above line to:
<?xml version="1.0" encoding="UTF-8"?>
<% response.setContentType("application/xhtml+xml;charset=UTF-8"); %>
<% request.setCharacterEncoding("UTF-8"); %>
The page stops rendering and throws XML parsing error such as "semi colon expected somewhere in my javascript" or "processing instruction not found" etc etc.
Removing "charset=utf-8" from response.setContentType makes the page render. The only problem is that → shows up as a questin mark "?"
Upvotes: 1
Views: 2009
Reputation: 100220
You probably have code like this:
<script type="text/javascript"> if (a && b) </script>
which is forbidden in XHTML mode, but required in text/html
mode. You'll find explanation of this problem in Sending XHTML as text/html Considered Harmful.
And code like:
<a href="foo?bar&baz">
is not allowed in any version of HTML or XHTML. It must always be written as:
<a href="foo?bar&baz">
Apparently you're not generating page using XML serializer (it wouldn't let you create invalid entities or improperly encoded characters), therefore I suggest that you use HTML 4 Strict or HTML5-as- text/html
instead, which are more appropriate for hand-coded markup.
Upvotes: 1
Reputation: 1109875
The page stops rendering and throws XML parsing error such as "semi colon expected somewhere in my javascript" or "processing instruction not found" etc etc.
That will happen if you declare XHTML as XML instead of "HTML with XML syntax". Indeed get rid of that XML declaration. If you can, I'd go a step further and just use HTML as real HTML, i.e. use <!doctype html>
or any other HTML strict
doctype. Also see http://hsivonen.iki.fi/doctype/.
<% request.setCharacterEncoding("UTF-8"); %>
First detail is that the request.setCharacterEncoding("UTF-8")
is way superfluous. At this stage it's already too late to set that. Second detail is that you're using scriptlets for that. I recommend not to do so. Use taglibs/EL where appropriate. If that's not possible, then the code logic actually belongs in a Java class, either directly or indirectly in a Servlet or Filter class.
Removing "charset=utf-8" from response.setContentType makes the page render. The only problem is that → shows up as a questin mark "?"
The response.setContentType(..)
is superfluous if you already set it as a <meta>
tag in the HTML <head>
which is imho much cleaner.
Finally you also need to set the response character encoding (that's different from setting the content type!) as follows:
<%@ page pageEncoding="UTF-8" %>
This by the way also implicitly creates a <meta>
tag for the content-type
. More background information and hints can be found here.
Hope this helps.
Upvotes: 1