效果

开发结构参考AJAX,JSON用户校验

主要有两个核心文件

1,处理输入字符,进行后台搜索的servlet

linkage.java

 package org.guangsoft.servlet;

 import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.guangsoft.bean.Address;
import org.guangsoft.dao.SqlHelper; import com.google.gson.Gson; public class Linkage extends HttpServlet
{ public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
} protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
String provinceNo = request.getParameter("provinceNo");
String cityNo = request.getParameter("cityNo");
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet resultSet = null;
try
{
String sql = "";
connection = SqlHelper.getConnection();
if(cityNo != null && cityNo != "")
{
sql = "select code,name from area where citycode = ?";
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1,cityNo);
}
else if(provinceNo != null && provinceNo != "")
{
sql = "select code,name from city where provincecode = ?";
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1,provinceNo);
}
else
{
sql = "select code,name from province";
prepareStatement = connection.prepareStatement(sql);
} resultSet = prepareStatement.executeQuery();
List<Address> addrList = new ArrayList<Address>();
while(resultSet.next())
{
Address address = new Address();
address.setAreaNo(resultSet.getString("code"));
address.setAreaName(resultSet.getString("name"));
addrList.add(address);
}
Gson gson = new Gson();
response.getWriter().print(gson.toJson(addrList));
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
SqlHelper.close(connection, prepareStatement, resultSet);
}
} }

2,展示前台

linkage.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">
<html>
<head>
<base href="<%=basePath%>">
<script src="/ajax/js/AjaxUtil.js"></script>
<script>
function getProvince()
{
sendAjaxReq("get","linkage",null,function(data)
{
eval("var addrList="+data);
var province = document.getElementById("province");
for(var i = 0; i < addrList.length; i++)
{
var address = addrList[i];
province.innerHTML += "<option value="+address.areaNo+">"+address.areaName+"</option>";
}
});
}
function getCity(provinceNo)
{
sendAjaxReq("get","/ajax/linkage?provinceNo="+provinceNo,null,function(data)
{
eval("var addrList="+data);
var city = document.getElementById("city");
city.innerHTML="<option value='0'>---请选择---</option>";
for(var i = 0; i < addrList.length; i++)
{
var address = addrList[i];
city.innerHTML += "<option value="+address.areaNo+">"+address.areaName+"</option>";
}
});
}
function getArea(cityNo)
{
sendAjaxReq("get","/ajax/linkage?cityNo="+cityNo,null,function(data)
{
eval("var addrList="+data);
var area = document.getElementById("area");
area.innerHTML="<option value='0'>---请选择---</option>";
for(var i = 0; i < addrList.length; i++)
{
var address = addrList[i];
area.innerHTML += "<option value="+address.areaNo+">"+address.areaName+"</option>";
}
});
}
</script>
</head> <body onload="getProvince();">
省:<select id="province" onchange="getCity(this.value);">
<option value="0">---请选择---</option>
</select>
市:<select id="city" onchange="getArea(this.value);">
<option value="0">---请选择---</option>
</select>
区:<select id="area">
<option value="0">---请选择---</option>
</select>
</body>
</html>

开发截图

AJAX省市县三级联动的更多相关文章

  1. AJAX省市县三级联动的实现

    省市县数据 本例子中省市县数据保存在MySQL数据库中,部分数据截图如下: 从数据库中读取数据 导入需要的jar包 连接池配置文件 <c3p0-config> <!-- 默认配置,如 ...

  2. jquery+php+mysql实现Ajax省市县三级联动

    1.第一步建立一个html页面的,放置省.市.县三个select选择框,代码如下: <!DOCTYPE html> <html> <head> <title& ...

  3. php仿经典省市县三级联动

    之前有个需求要写个类似省市县三级联动的页面,于是,网上找了点资料看了下,其实原理很简单: 当我们选择一级栏目中某条记录的时候,会获取该栏目的vaule值,并发起ajax请求,后台根据这个vaule值, ...

  4. 项目总结01:JSP mysql SpringMvc下中国省市县三级联动下拉框

    JSP mysql SpringMvc下中国省市县三级联动下拉框 关键词 JSP  mysql数据库  SpringMvc  ajax   Controller层  Service层  中国地区  省 ...

  5. jQuery - 全国省市县三级联动

    最近有空用jquery做了一个全国省市县的三级联动,在以后或许可以用的到 ,遗憾的是我还没用封装,等有空看能不能封装成一个插件 废话不多说,贴上代码: <!doctype html> &l ...

  6. wex5 实战 省市县三级联动与地址薄同步

    无论是商城,还是快递,都要用到省市县三级联动,和地址薄,今天就以实战来制作,难点有3个: 1:三级联动,有wex5组件实现,相对简单,实战里对行数据进行了拼接 2:  地址薄选项,利用inputSel ...

  7. 省市县三级联动 sql语句

    发现在网上的省市县三级联动大部分是mysql的.就算是sqlserver的,也不准确.于是就把mysql的给改了下,适用sqlserver.sql语句如下: CREATE TABLE Dic_Area ...

  8. Android 省市县 三级联动(android-wheel的使用)[转]

    转载:http://blog.csdn.net/lmj623565791/article/details/23382805 今天没事跟群里面侃大山,有个哥们说道Android Wheel这个控件,以为 ...

  9. Android 省市县 三级联动(android-wheel的使用)

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23382805 今天没事跟群里面侃大山,有个哥们说道Android Wheel这个 ...

随机推荐

  1. USACO 3.2 ratios 高斯消元

    题目原意很简单,就是解一个三元一次方程组 直接高斯消元解方程组,枚举最后一列的倍数(k) 注意double的精度,有很多细节需要处理 /* PROB:ratios LANG:C++ */ #inclu ...

  2. Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server

    目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...

  3. Ecshop /admin/get_password.php Password Recovery Secrect Code Which Can Predict Vulnerability

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Ecshop提供了密码找回功能,但是整个密码找回流程中存在一些设计上的安全隐患 . ...

  4. Linux LSM(Linux Security Modules) Hook Technology

    目录 . 引言 . Linux Security Module Framework Introduction . LSM Sourcecode Analysis . LSMs Hook Engine: ...

  5. 清除TFS版本控制信息

    http://blog.csdn.net/feihu_guest/article/details/8442434 How to permanently remove TFS Source Contro ...

  6. 《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中

    http://blog.csdn.net/mazhaojuan/article/details/8599167 先看一下我要实现的功能界面: 这篇文章主要介绍:后台一般处理程序把从数据库查找的数据,转 ...

  7. app工程构成

    1)工程配置文件: 2)源代码: 3)资源文件.

  8. jquery------添加jQuery对象方法

    my.js $(document).ready(function(){ (function($){ $.fn.swapClass=function(class1,class2){ if(this.ha ...

  9. PRML Chapter 2. Probability Distributions

    PRML Chapter 2. Probability Distributions P68 conjugate priors In Bayesian probability theory, if th ...

  10. 将对象转为数组方法:延伸array_map函数在PHP类中调用内部方法

    public static function objectToArray($d) { if (is_object($d)) { $d = get_object_vars($d); } if (is_a ...