AJax技术

JavaWeb(9):AJAX(4)

字号+ 作者:H5之家 来源:H5之家 2017-09-02 14:04 我要评论( )

准备处理逻辑,三个Servlet 1 package com.servlet; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 im

准备处理逻辑,三个Servlet

1 package com.servlet; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.model.Province; 13 import com.util.DataBaseUtil; 14 15 /** 16 * Servlet implementation class SelectAllProServlet 17 */ 18 @WebServlet("/SelectAllProServlet") 19 public class SelectAllProServlet extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public SelectAllProServlet() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 response.setCharacterEncoding("utf-8"); 35 //初始化数据库操作类 36 DataBaseUtil db = new DataBaseUtil(); 37 38 List<Province> plist = db.SelectAllPro(); 39 40 String json = "{\"success\":"; 41 42 if (plist != null) { 43 json += "true,\"result\":";//result是一个键,他得值是后面的数组,这个数组包含了一串键值对 44 json += "["; 45 for (int i = 0; i < plist.size(); i++) { 46 json += "{\"id\":\"" + plist.get(i).getId() + "\",\"province\":\"" + plist.get(i).getProvince() 47 + "\",\"provinceid\":\"" + plist.get(i).getProvinceid() + "\"}"; 48 if (i != plist.size() - 1) { 49 json += ","; 50 } 51 } 52 json += "]}"; 53 } else { 54 json += "false}"; 55 } 56 response.getWriter().append(json); 57 } 58 59 /** 60 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 61 */ 62 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 63 // TODO Auto-generated method stub 64 doGet(request, response); 65 } 66 67 } 1 package com.servlet; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.alibaba.fastjson.JSONObject; 13 import com.model.City; 14 import com.util.DataBaseUtil; 15 16 /** 17 * Servlet implementation class SelectCityServlet 18 */ 19 @WebServlet("/SelectCityServlet") 20 public class SelectCityServlet extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 /** 24 * @see HttpServlet#HttpServlet() 25 */ 26 public SelectCityServlet() { 27 super(); 28 // TODO Auto-generated constructor stub 29 } 30 31 /** 32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 35 response.setCharacterEncoding("utf-8"); 36 37 String provinceid = request.getParameter("provinceid"); 38 39 DataBaseUtil db = new DataBaseUtil(); 40 41 List<City> clist = db.SelectCity(provinceid); 42 /* 43 * 拼接字符串传回json 44 * System.out.println(provinceid); 45 String json = "{\"success\":"; 46 47 if (clist != null) { 48 json += "true,\"result\":"; 49 json += "["; 50 for (int i = 0; i < clist.size(); i++) { 51 json += "{\"id\":\"" + clist.get(i).getId() + "\",\"city\":\"" + clist.get(i).getCity() 52 + "\",\"cityid\":\"" + clist.get(i).getCityid() + "\"}"; 53 if (i != clist.size() - 1) { 54 json += ","; 55 } 56 } 57 json += "]}"; 58 } else { 59 json += "false}"; 60 } 61 62 response.getWriter().append(json);*/ 63 //导包调用方法,传回json 64 JSONObject jo = new JSONObject(); 65 66 if(clist!=null) { 67 jo.put("success", true); 68 jo.put("result", clist); 69 } else { 70 jo.put("success", false); 71 } 72 response.getWriter().append(jo.toJSONString()); 73 } 74 75 /** 76 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 77 */ 78 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 79 // TODO Auto-generated method stub 80 doGet(request, response); 81 } 82 83 } 1 package com.servlet; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.alibaba.fastjson.JSONObject; 13 import com.model.Area; 14 import com.model.City; 15 import com.util.DataBaseUtil; 16 17 /** 18 * Servlet implementation class SelectareaServlet 19 */ 20 @WebServlet("/SelectareaServlet") 21 public class SelectareaServlet extends HttpServlet { 22 private static final long serialVersionUID = 1L; 23 24 /** 25 * @see HttpServlet#HttpServlet() 26 */ 27 public SelectareaServlet() { 28 super(); 29 // TODO Auto-generated constructor stub 30 } 31 32 /** 33 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 34 */ 35 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 response.setCharacterEncoding("utf-8"); 37 38 String cityid = request.getParameter("cityid"); 39 40 DataBaseUtil db = new DataBaseUtil(); 41 42 List<Area> alist = db.SelectArea(cityid); 43 JSONObject jo = new JSONObject(); 44 45 if(alist!=null) { 46 jo.put("success", true); 47 jo.put("result", alist); 48 } else { 49 jo.put("success", false); 50 } 51 response.getWriter().append(jo.toJSONString()); 52 53 } 54 55 /** 56 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 57 */ 58 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 59 // TODO Auto-generated method stub 60 doGet(request, response); 61 } 62 63 }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  •  Ajax学习札记01-简单实现数据异步交互

    Ajax学习札记01-简单实现数据异步交互

    2017-09-02 15:00

  • 前端学习之ajax

    前端学习之ajax

    2017-09-02 14:04

  • Xajax相关资料

    Xajax相关资料

    2017-09-02 08:02

  • 她的技巧qvod|涕泗横流|ajax实例

    她的技巧qvod|涕泗横流|ajax实例

    2017-09-01 17:01

网友点评
/