日期:2014-05-19 浏览次数:20687 次
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter printWriter = response.getWriter(); response.setHeader("Content-Type", "text/xml ; charset=utf-8"); response.setContentType("text/xml ; charset=utf-8"); response.setCharacterEncoding("text/xml ; charset=utf-8"); Document document =DocumentHelper.createDocument(); Element rootElement = document.addElement("person"); count++; Element messagElement = rootElement.addElement("message"); String str ="登录用户名"+name+"在线人数:"+count; byte[] bytes = str.getBytes("utf-8"); String returnstring = new String(bytes,0,bytes.length,"utf-8"); System.out.println(returnstring); messagElement.setText(returnstring); OutputFormat format = OutputFormat.createCompactFormat(); format.setEncoding("utf-8"); XMLWriter xmlWriter = new XMLWriter(printWriter,format); xmlWriter.write(document); printWriter.flush();