省市联动,xml传值方式
导入相应的jar
创建表单
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html >
<head >
<meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
<title > Insert title here</title >
<script type ="text/javascript" src ="js/jquery-1.11.3.min.js" > </script >
// 这个是用xml传数据 的js
<script type ="text/javascript" src ="js/cityjs.js" > </script >
</head >
<body >
//向后端传value的值,进行查找
<select id ="vv" >
<option > --请选择--</option >
<option value ="1" > --河南--</option >
<option value ="2" > --苏州--</option >
<option value ="3" > --广东--</option >
</select >
<select id ="c" >
<option > --请选择--</option >
</select >
</body >
</html >
创建js
$(function () {
$("#vv" ).change(function () {
var pid = $(this ).val();
$.post( "/checkUsername/cityservlet" ,{pid:pid} ,function (data,status) {
$("#c" ).html("<option >--check--" )
$(data).find("city" ).each(function () {
var id = $(this ).children("id" ).text();
var cname = $(this ).children("cname" ).text();
$("#c" ).append("<option value='" +id+"' >" +cname)
});
} );
});
});
创建servlet,dao
try {
request. setCharacterEncoding("UTF-8" );
int pid= Integer . parseInt(request. getParameter("pid" ));
checkdao c= new checkImpl();
List < city> list = c. findcity(pid);
XStream xs= new XStream();
xs. alias("city" , city. class);
String xml = xs. toXML(list );
System. out. println(xml );
response. setContentType("text/xml;charset=utf-8" );
response. getWriter(). write(xml );
} catch (SQLException e) {
e. printStackTrace();
}
}
省市联动,json传值方式
导入相应的jar
创建表单
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html >
<head >
<meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
<title > Insert title here</title >
//这是json数据的js
<script type ="text/javascript" src ="js/jquery-1.11.3.min.js" > </script >
<script type ="text/javascript" src ="js/cityjson.js" > </script >
</head >
<body >
<select id ="vv" >
<option > --请选择--</option >
<option value ="1" > --河南--</option >
<option value ="2" > --苏州--</option >
<option value ="3" > --广东--</option >
</select >
<select id ="c" >
<option > --请选择--</option >
</select >
</body >
</html >
创建js
$(function () {
$("#vv" ).change(function () {
var pid=$(this ).val();
$.post("/checkUsername/jsonservlet" ,{pid:pid},function (data,status) {
$("#c" ).html("<option value='' >--check--" );
$(data).each(function (i,c) {
$("#c" ).append("<option value='" +c.pid+"'>" +c.cname);
});
},"json" );
});
});
创建servlet,dao
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
request.setCharacterEncoding ("UTF-8" )
int pid=Integer.parseInt (request.getParameter ("pid" ))
checkdao c=new checkImpl()
List<city> list = c.findcity (pid)
JSONArray ja=JSONArray.fromObject (list)
String json = ja.toString ()
System.out .println (json)
response.setContentType ("text/xml;charset=utf-8" )
response.getWriter ().write (json)
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace ()
}
}
}