Jquery+jsp to achieve the three-level linkage effect (with source code) of provinces and counties _jquery

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.