我正在努力确保我的数据路径 - 一个Tomcat servlet通过JDBC将数据导入/导出MySQL数据库 - 直接处理Unicode。
我已经能够验证我是否可以从数据库中读取/写入Unicode。 (当我在Eclipse中调试Tomcat时,我看到正确地从数据库中检索到的结果。)但是当我将浏览器指向我的Tomcat servlet时,像“García”(= Garci {U + 0301} a)这样的字符串变成了“Garci” ?a“在浏览器中。
我正在使用此代码片段初始化XML输出(request
和response
,它们使用XMLStreamWriter,我将结果声明为UTF-8:
final protected HttpServletRequest request;
final protected HttpServletResponse response;
...
boolean handleRefreshMetadata()
{
String s = request.getParameter("ids");
Integer id = Integer.parseInt(s);
boolean b = refreshMetadata(id);
response.setContentType("text/xml");
try {
PrintWriter writer = response.getWriter();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter xmlwriter = factory.createXMLStreamWriter(writer);
xmlwriter.writeStartDocument("UTF-8", "1.0");
xmlwriter.writeStartElement("response");
xmlwriter.writeAttribute("success", b ? "true" : "false");
if (b && (id != null))
{
loadArticleFromID(getConnection(), xmlwriter, id);
}
xmlwriter.writeEndDocument();
xmlwriter.flush();
xmlwriter.close();
} catch (IOException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
return b;
}
我错过了什么吗?
答案 0 :(得分:4)
Darnit,我想通了:
而不是
response.setContentType("text/xml");
我需要这样做:
response.setContentType("text/xml; charset=utf-8");
答案 1 :(得分:0)
您的内容未经过unicode编码。对响应内容进行编码,如下所示:
final javax.servlet.http.HttpServletResponse resp = (HttpServletResponse)ctx.getExternalContext().getResponse();
byte[] k =xml.getBytes(UTF8_CHARSET); // xml is the string with unicode content
resp.setContentType("text/xml");
resp.setContentLength(k.length);
resp.getOutputStream().write(k);
resp.getOutputStream().flush();
resp.getOutputStream().close();