Ajax achieves provincial/municipal level-3 cascade (data comes from the mysql database ),
Implementation of Ajax to achieve provincial/municipal level-3 cascade, requires Java parsing json Technology
The overall Demo is as follows: Click to download
Address.html
<! DOCTYPE html>
AddressServlet. java
Package cn. bestchance. servlet; import java. io. IOException; import java. util. arrayList; import javax. servlet. servletException; import javax. servlet. annotation. webServlet; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import cn. bestchance. dao. addressDao; import cn. bestchance. dao. impl. addressDaoImpl; import cn. bestchance. entity. address; import net. sf. json. JSONArray; import net. sf. json. JSONObject; @ WebServlet ("/addressSerlvet") public class AddressSerlvet extends HttpServlet {private static final long serialVersionUID = 1L; private AddressDao dao = new Login (); protected void doGet (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {doPost (request, response);}/*** @ see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse * response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); String method = request. getParameter ("method"); if ("provincial ". equals (method) {getProvincial (request, response);} if ("city ". equals (method) {getCity (request, response);} if ("area ". equals (method) {getArea (request, response );}} /*** obtain all information of the Zone in the city according to the city id * @ param request * @ param response * @ throws ServletException * @ throws IOException */protected void getArea (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {String cityId = request. getParameter ("cityId"); // query the province information from the database. ArrayList <Address> areaList = dao. getAreaByCityId (Integer. parseInt (cityId); // convert the set to a json string JSONObject jsonObj = new JSONObject (); JSONArray jsonArray = JSONArray. fromObject (areaList); jsonObj. put ("areaList", jsonArray); String jsonDataStr = jsonObj. toString (); response. getWriter (). print (jsonDataStr );} /*** get the province information and corresponding * @ param request * @ param response * @ throws ServletException * @ throws IOException */protected void getProvincial (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// query the province information from the database ArrayList <Address> addrList = dao. getProvince (); // convert the set to a json string JSONObject jsonObj = new JSONObject (); JSONArray jsonArray = JSONArray. fromObject (addrList); jsonObj. put ("addrList", jsonArray); String jsonDataStr = jsonObj. toString (); response. getWriter (). print (jsonDataStr );} /*** get the city information and corresponding * @ param request * @ param response * @ throws ServletException * @ throws IOException */protected void getCity (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String provinceId = request. getParameter ("provincial"); // query the province information from the database. ArrayList <Address> addrList = dao. getCityByProvinceId (Integer. parseInt (provinceId); // convert the set to a json string JSONObject jsonObj = new JSONObject (); JSONArray jsonArray = JSONArray. fromObject (addrList); jsonObj. put ("cityList", jsonArray); String jsonDataStr = jsonObj. toString (); response. getWriter (). print (jsonDataStr );}}
AddressDao. java
Package cn. bestchance. dao; import java. util. arrayList; import cn. bestchance. entity. address; public interface AddressDao {/*** get the province id and name * @ return */ArrayList <Address> getProvince (); /*** obtain city information by province id * @ param provinceId * @ return */ArrayList <Address> getCityByProvinceId (int provinceId ); /*** obtain the region information based on the city id * @ param cityId * @ return */ArrayList <Address> getAreaByCityId (int cityId );}
AddressDaoImpl. java
package cn.bestchance.dao.impl;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import cn.bestchance.dao.AddressDao;import cn.bestchance.entity.Address;import cn.bestchance.util.DBUtil;public class AddressDaoImpl implements AddressDao { private DBUtil db = new DBUtil(); @Override public ArrayList<Address> getProvince() { ArrayList<Address> addrList = new ArrayList<Address>(); db.openConnection(); String sql = "select * from province"; ResultSet rs = db.excuteQuery(sql); try { while(rs.next()){ Address addr = new Address(); addr.setId(rs.getInt(2)); addr.setAddress(rs.getString(3)); addrList.add(addr); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(rs != null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } db.closeResoure(); } return addrList; } @Override public ArrayList<Address> getCityByProvinceId(int provinceId) { ArrayList<Address> addrList = new ArrayList<Address>(); db.openConnection(); String sql = "select * from city where fatherID = " + provinceId; //431200 ResultSet rs = db.excuteQuery(sql); try { while(rs.next()){ Address addr = new Address(); addr.setId(rs.getInt(2)); addr.setAddress(rs.getString(3)); addrList.add(addr); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(rs != null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } db.closeResoure(); } return addrList; } @Override public ArrayList<Address> getAreaByCityId(int cityId) { ArrayList<Address> addrList = new ArrayList<Address>(); db.openConnection(); String sql = "select * from area where fatherID = " + cityId; //431200 ResultSet rs = db.excuteQuery(sql); try { while(rs.next()){ Address addr = new Address(); addr.setId(rs.getInt(2)); addr.setAddress(rs.getString(3)); addrList.add(addr); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(rs != null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } db.closeResoure(); } return addrList; }}
Entity Class Address. java
package cn.bestchance.entity;public class Address { @Override public String toString() { return "Address [id=" + id + ", address=" + address + "]"; } private int id; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Address() { super(); // TODO Auto-generated constructor stub } public Address(int id, String address) { super(); this.id = id; this.address = address; }}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.