helloPie.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package BA_Control;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.ArrayList;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import com.helloBar.barBean;
  10. import com.helloBar.barDAO;
  11. import net.sf.json.JSONArray;
  12. public class helloPie extends HttpServlet {
  13. /**
  14. * Constructor of the object.
  15. */
  16. public helloPie() {
  17. super();
  18. }
  19. /**
  20. * Destruction of the servlet. <br>
  21. */
  22. public void destroy() {
  23. super.destroy(); // Just puts "destroy" string in log
  24. // Put your code here
  25. }
  26. /**
  27. * The doGet method of the servlet. <br>
  28. *
  29. * This method is called when a form has its tag value method equals to get.
  30. *
  31. * @param request the request send by the client to the server
  32. * @param response the response send by the server to the client
  33. * @throws ServletException if an error occurred
  34. * @throws IOException if an error occurred
  35. */
  36. public void doGet(HttpServletRequest request, HttpServletResponse response)
  37. throws ServletException, IOException {
  38. response.setContentType("text/html");
  39. PrintWriter out = response.getWriter();
  40. out
  41. .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  42. out.println("<HTML>");
  43. out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  44. out.println(" <BODY>");
  45. out.print(" This is ");
  46. out.print(this.getClass());
  47. out.println(", using the GET method");
  48. out.println(" </BODY>");
  49. out.println("</HTML>");
  50. out.flush();
  51. out.close();
  52. }
  53. /**
  54. * The doPost method of the servlet. <br>
  55. *
  56. * This method is called when a form has its tag value method equals to post.
  57. *
  58. * @param request the request send by the client to the server
  59. * @param response the response send by the server to the client
  60. * @throws ServletException if an error occurred
  61. * @throws IOException if an error occurred
  62. */
  63. public void doPost(HttpServletRequest request, HttpServletResponse response)
  64. throws ServletException, IOException {
  65. //鍒涘缓浜嗕竴涓猙ardao鐨勫璞★紝barDAO涓昏鏄鏁版嵁搴撶殑杩炴帴鍜屽鏁版嵁搴撶殑鎿嶄綔
  66. barDAO bardao=new barDAO();
  67. //璋冪敤bardao鐨剆elect_all()鏂规硶鎶婁粠鏁版嵁搴撲腑璇诲彇鎵�鏈夌殑鏁版嵁杩斿洖鐨勬槸涓�涓狝rrayList锛孉rrayList閲岄潰鏀剧殑鏄竴涓猙arBean
  68. ArrayList<barBean> array = (ArrayList<barBean>) bardao.listAll();
  69. System.out.println("s"+array);
  70. //璁剧疆杩斿洖鏃剁殑缂栫爜鏍煎紡
  71. response.setContentType("text/html; charset=utf-8");
  72. //璋冪敤JSONArray.fromObject鏂规硶鎶奱rray涓殑瀵硅薄杞寲涓篔SON鏍煎紡鐨勬暟缁�
  73. JSONArray json=JSONArray.fromObject(array);
  74. System.out.println(json.toString());
  75. //杩斿洖缁欏墠娈甸〉闈�
  76. PrintWriter out = response.getWriter();
  77. out.println(json);
  78. out.flush();
  79. out.close();
  80. }
  81. /**
  82. * Initialization of the servlet. <br>
  83. *
  84. * @throws ServletException if an error occurs
  85. */
  86. public void init() throws ServletException {
  87. // Put your code here
  88. }
  89. }