我们此时只要写一个Servlet就可以了:
1 public class UpdateMenuServlet extends HttpServlet { 2 /** 3 * 4 */ 5 private static final long serialVersionUID = 1L; 6 // 构造方法 7 public UpdateMenuServlet() { 8 super(); 9 } 10 // 销毁方法 11 public void destroy() { 12 super.destroy(); 13 } 14 // 响应Get请求 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 response.setContentType("text/xml"); 18 PrintWriter out = response.getWriter(); 19 // 实例化dao 20 UpdateDao dao = new UpdateDaoImpl(); 21 // 获得菜谱列表 22 List<Menu> list = dao.getMenuList(); 23 24 // 拼XML格式数据 25 out.println("<?xml version='1.0' encoding='UTF-8'?>"); 26 // 根节点 27 out.println("<menulist>"); 28 for (int i = 0; i <list.size(); i++) { 29 Menu m = list.get(i); 30 out.println("<menu>"); 31 // 菜谱编号 32 out.print("<id>"); 33 out.print(m.getId()); 34 out.println("</id>"); 35 // 分类 36 out.print("<typeId>"); 37 out.print(m.getTypeId()); 38 out.println("</typeId>"); 39 // 名称 40 out.print("<name>"); 41 out.print(m.getName()); 42 out.println("</name>"); 43 // 图片路径 44 out.print("<pic>"); 45 out.print(m.getPic()); 46 out.println("</pic>"); 47 // 价格 48 out.print("<price>"); 49 out.print(m.getPrice()); 50 out.println("</price>"); 51 // 备注 52 out.print("<remark>"); 53 out.print(m.getRemark()); 54 out.println("</remark>"); 55 56 out.println("</menu>"); 57 } 58 out.println("</menulist>"); 59 out.flush(); 60 out.close(); 61 } 62 // 响应Post请求 63 public void doPost(HttpServletRequest request, HttpServletResponse response) 64 throws ServletException, IOException { 65 doGet(request,response); 66 } 67 // 初始化方法 68 public void init() throws ServletException { 69 } 70 }
然后就是直接在浏览器中输入这个Servlet的具体网址就可以了:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 - <menulist> 3 - <menu> 4 <id>1</id> 5 <typeId>1</typeId> 6 <name>水煮鱼</name> 7 <pic>test</pic> 8 <price>20</price> 9 <remark>test</remark> 10 </menu> 11 - <menu> 12 <id>2</id> 13 <typeId>2</typeId> 14 <name>凉拌西红柿</name> 15 <pic>tets</pic> 16 <price>10</price> 17 <remark>test</remark> 18 </menu> 19 - <menu> 20 <id>3</id> 21 <typeId>3</typeId> 22 <name>tofu</name> 23 <pic>test</pic> 24 <price>8</price> 25 <remark>test</remark> 26 </menu> 27 </menulist>