准备工作:导入所需要的文件
前端页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
$(function() {
$.ajax({
type : "post",
url : "getInfo?method=province",
success : function(data) {
var result = JSON.parse(data);
result = result.province;
if (result.resultCode==1) {
$("#province").get(0).options.length = result.length + 1;
$("#province").get(0).options[0] = new Option("请选择", 0);
for (var i = 0; i < result.length; i++) {
$("#province").get(0).options[i + 1] = new Option(
result[i].name, result[i].id);
}
// {"province":[{"id":1,"name":"江西"},{"id":2,"name":"江苏"}]
}else{
alert("服务器错误!") ;
}
}
}
});
});
function loadCity(provinceId) {
$.ajax({
type : "post",
url : "getInfo?method=city",
data : "provinceId=" + provinceId,
success : function(data) {
var result = JSON.parse(data);
result = result.citys;
$("#city").get(0).options.length = result.length + 1;
$("#city").get(0).options[0] = new Option("请选择", 0);
for (var i = 0; i < result.length; i++) {
$("#city").get(0).options[i + 1] = new Option(
result[i].name, result[i].id);
}
// {"province":[{"id":1,"name":"江西"},{"id":2,"name":"江苏"}]
}
});
}
省份:
城市:
package com.zt.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import com.zt.dao.ProvinceDao;
import com.zt.dao.impl.ProvinceDaoImpl;
import com.zt.po.City;
import com.zt.po.Province;
public class GetInfoServlet extends HttpServlet {
ProvinceDao provin =null;
@Override
protected void service(HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
String methodString=request.getParameter("method");
if (methodString.equals("province")) {
province(request, response);
}
if (methodString.equals("city")) {
city(request, response);
}
}
protected void province(HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
List provinces=provin.findAllProvince();
JSONObject jsonObject=new JSONObject(); //创建一个JSONObject对象
if (provinces.size()>0) {
jsonObject.element("province",provinces);//将provinces存进JSONObject对象中
jsonObject.element("resultCode",1);//将返回结果成功返回1存进JSONObject对象中
}else{
jsonObject.element("resultCode",0);//将返回结果失败返回0存进JSONObject对象中
}
PrintWriter out =response.getWriter();
out.print(jsonObject.toString());//调用JSONObject对象的toString方法,然后输出
out.flush();
out.close();
}
protected void city(HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
String provinceIdString=request.getParameter("provinceId");
int province=0;
if (provinceIdString!=null&&!"".equals(provinceIdString)) {
province=Integer.parseInt(provinceIdString);
}
List citys=provin.findAllProvinceCity(province);
JSONObject jsonObject=new JSONObject();
jsonObject.element("citys", citys);
PrintWriter out =response.getWriter();
out.print(jsonObject.toString());
out.flush();
out.close();
}
@Override
public void init() throws ServletException {
provin=new ProvinceDaoImpl();
}
}