This paper illustrates the method of realizing the three-level linkage effect of jquery+jsp in provinces and counties. Share to everyone for your reference, specific as follows:
Here, the MySQL database is used to store all the provinces and counties in the country information (click here to download the source code)
Used jar Packs
Google's Gson.jar
Mysql-connector-java-5.1.13-bin.jar
Post the Experiment diagram:
Display Page index.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath ()
;
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Database Interaction Getdropdowndataservlet
public class Getdropdowndataservlet extends HttpServlet {public void doget (HttpServletRequest request, HTTPSERVLETRESPO
NSE response) throws IOException, servletexception {doPost (request, response);
public void DoPost (HttpServletRequest request, httpservletresponse response) throws IOException, Servletexception {
String parentid = Request.getparameter ("ParentID");
if (ParentID = null | | parentid = = "") {ParentID = "0";
} Connection conn = null;
String json = "";
try {class.forname ("com.mysql.jdbc.Driver");
conn = Drivermanager.getconnection ("Jdbc:mysql://localhost/dropdown", "root", "root");
Statement stat = conn.createstatement ();
ResultSet rs = Stat. executequery ("Select Region_id,region_name from region where parent_id =" + ParentID);
ArrayList rslist = new ArrayList ();
Map map = null;
while (Rs.next ()) {map = new HashMap ();
Map.put ("id", Rs.getint (1));
Map.put ("Name", Rs.getstring (2)); Rslist.aDD (map);
rs = null;
Gson Gson = new Gson ();
JSON = Gson.tojson (rslist);
System.out.println ("json=" + JSON);
catch (ClassNotFoundException e) {e.printstacktrace ();
catch (SQLException e) {e.printstacktrace ();
finally {if (conn!= null) {try {conn.close ();
catch (SQLException e) {e.printstacktrace ();
}} response.setcharacterencoding ("UTF-8");
Response.getwriter (). print (JSON);
}
}
I hope this article will help you with your jquery programming.