/** * Constructor of the object. */ public buildXMLAction() { super(); }
/** * Destruction of the servlet.
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here }
/** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response); }
/** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/xml"); PrintWriter out = response.getWriter(); // 创建根节点 list; Element root = new Element("list");
// 根节点添加到文档中; Document Doc = new Document(root);
// 此处 for 循环可替换成 遍历 数据库表的结果集操作; for (int i = 0; i < 5; i++) {
// 创建节点 user; Element elements = new Element("user");
// 给 user 节点添加属性 id; elements.setAttribute("id", "" + i);
// 给 user 节点添加子节点并赋值; // new Element("name")中的 "name" 替换成表中相应字段,setText("xuehui")中 "xuehui 替换成表中记录值; elements.addContent(new Element("name"+i).setText("xuehui"+i)); elements.addContent(new Element("age"+i).setText("28"+i)); elements.addContent(new Element("sex"+i).setText("Male"+i));