一:准备工作
数据库,在城市表中省份id作为外键
pom. xml文件
javax.servlet javax.servlet-api3.0.1 javax.servlet.jsp jsp-api2.2 mysql mysql-connector-java5.0.5 jstl jstl1.2 commons-beanutils commons-beanutils1.9.3 commons-collections commons-collections3.2.1 commons-lang commons-lang2.6 commons-logging commons-logging1.1.1 net.sf.ezmorph ezmorph1.0.6 net.sf.json-lib json-lib2.4 jdk13
maven工程结构
index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
接下来我们完成dao层,service层,action,util,jsp
ProvinceCityDao
public interface ProvinceCityDao {
public List getProvince();
public List getCity(String codePid);//省份的id
}
ProvinceCityService
public interface ProvinceCityService {
public List getProvince();
public List getCity(String codePid);//省份的id
}
ProvinceCityServiceImpl
public class ProvinceCityServiceImpl implements ProvinceCityService{
ProvinceCityDao provinceCityDao = new ProvinceDaoImpl();
//查询省
public List getProvince() {
return provinceCityDao.getProvince();
}
//查询市
public List getCity(String codePid) {
return provinceCityDao.getCity(codePid);
}
}
ProvinceAction
@WebServlet("/getProvince")
public class ProvinceAction extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理请求编码格式 Request
req.setCharacterEncoding("UTF-8");
//处理 Response 响映json编码格式
resp.setContentType("application/json;charset=UTF-8");
ProvinceCityService provinceCityService = new ProvinceCityServiceImpl();
List list = provinceCityService.getProvince();
//citys转换成jsonArray格式
JSONArray jsonArray = JSONArray.fromObject(list);
resp.getWriter().println(jsonArray);
}
}
CityAction
@WebServlet("/getCity")
public class CityAction extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理请求编码格式 Request
req.setCharacterEncoding("UTF-8");
//处理 Response 响映json编码格式'
resp.setContentType("application/json;charset=UTF-8");
//获取参数
String codePid = req.getParameter("id");
//创建控制层
ProvinceCityService provinceCityService = new ProvinceCityServiceImpl();
List list = provinceCityService.getCity(codePid);
//citys转换成jsonArray格式
JSONArray jsonArray = JSONArray.fromObject(list);
resp.getWriter().println(jsonArray);
}
}
entity层
public class Province {
private String id;
private String codePid;
private String name;
public class City {
private String id;
private String codeCid;
private String name;
private String codePid;
util
public class BaseDao {
private static String driveName;
private static String url;
private static String username;
private static String password;
static {
init();
}
public static void init() {
//获取配置文件
InputStream inputStream = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
try {
properties.load(inputStream);
driveName = properties.getProperty("db.driverName");
url = properties.getProperty("db.url");
username = properties.getProperty("db.userName");
password = properties.getProperty("db.pwd");
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName(driveName);
conn = DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeAll(ResultSet rs, Statement sta, Connection conn){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (sta != null) {
try {
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
配置文件
db.driverName=com.mysql.jdbc.Driver db.url=jdbc:mysql://127.0.0.1:3306/mysql02?useUnicode=true&characterEncoding=utf-8 db.userName=root db.pwd=123456
最关键的index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
最后效果



