:
1. List. jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <HTML>
Ii. List. js
$ (Function () {$ provinceselect = $ ("# Province"); $ cityselect = $ ("# City"); $ countyselect = $ ("# county "); // display Level 1 $. post (".. /provinceservlet ", function (jsonarray) {showprovinces (jsonarray) ;}," JSON "); // display Level 2, add a response event $ provinceselect to the first drop-down list. change (function () {// you need to clear the city drop-down list for each request. Otherwise, the city drop-down list will be accumulated // deleted, except for $ cityselect. find ("option: Not (: First )"). remove (); // Delete the county drop-down list <option>, except for $ countyselect. find ("option: Not (: First )"). remove (); var pid = This. value; If (pid = "") return; $. post (".. /cityservlet ", {pid: PID}, function (jsonarray) {showcitys (jsonarray) ;}," JSON ") ;}); // display Level 3, add a click RESPONSE event $ cityselect to the drop-down list of level 2. change (function () {var cid = This. value; $. post (".. /countyservlet ", {CID: Cid}, function (jsonarray) {showcountys (jsonarray) ;}," JSON ") ;}); // display the province function showprovinces (jsonarray) {$. each (jsonarray, function () {var pid = This. PID; var pname = This. pname; $ provinceselect. append ('<option value = "' + PID + '">' + pname + '</option>') ;}// display the city function showcitys (jsonarray) {$. each (jsonarray, function () {var cid = This. CID; var cname = This. cname; $ cityselect. append ('<option value = "' + CID + '">' + cname + '</option>') ;}// display the county function showcountys (jsonarray) {$. each (jsonarray, function () {var tid = This. TID; var tname = This. tname; $ countyselect. append ('<option value = "' + TID + '">' + tname + '</option> ');});}});
3. servlet
public class ProvinceServlet extends HttpServlet {private static final long serialVersionUID = 1L;private ProvinceDao dao=new ProvinceDao();protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {List<Province> list = dao.getAll();String json=new Gson().toJson(list);System.out.println(json);response.setContentType("text/json;charset=utf-8");response.getWriter().write(json);}}
public class CityServlet extends HttpServlet {private static final long serialVersionUID = 1L;private CityDao cityDao=new CityDao();protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String pid = request.getParameter("pid");List<City> list = cityDao.getListByPid(pid);String json=new Gson().toJson(list);System.out.println(json);response.setContentType("text/json;charset=utf-8");response.getWriter().write(json);}}
public class CountyServlet extends HttpServlet {private static final long serialVersionUID = 1L;private CountyDao countyDao=new CountyDao();protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("11111111");String cid = request.getParameter("cid");System.out.println(cid);List<County> list = countyDao.getListByCid(cid);String json=new Gson().toJson(list);System.out.println(json);response.setContentType("text/json;charset=utf-8");response.getWriter().write(json);}}
4. DAO and bean
Three-Level Linkage