Displaying xhtml content in a jsp page

六眼飞鱼酱① 提交于 2019-12-07 16:29:20

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.

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&amp;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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!