日期:2014-05-17 浏览次数:20696 次
package com.panjun.servlets; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("unchecked") public class ShowListServlet extends HttpServlet { private static final long serialVersionUID = -6920365688165262694L; private final List list = new ArrayList(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/xml;charset=UTF-8"); PrintWriter out = resp.getWriter(); //生成xml文件返回给html页面 StringBuilder xml = new StringBuilder(); xml.append("<items>"); for (Object o : list) { xml.append("<item>").append(o).append("</item>"); } xml.append("</items>"); out.print(xml.toString()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override public void init() throws ServletException { list.add("aaaa"); list.add("bbbb"); list.add("cccc"); list.add("dddd"); } }
------解决方案--------------------
帮顶!
JSON