UTF8 encoded XHTML content in JSP

醉酒当歌 提交于 2019-12-07 07:21:21

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