package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 net.sf.json.JSONArray;
import com.pojo.City;
import com.pojo.Country;
public class MenuServlet extends HttpServlet {
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String uri = request.getRequestURI();
System.out.println("uri:"+uri);
String path = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
System.out.println("path:" + path);
if ("/getCity".equals(path)) {
String sheng = request.getParameter("sheng");
System.out.println(sheng);
// TO DO
// 根据传递过来的省的value值,返回相应的市
if ("gd".equals(sheng)) {
// 对象,属性
List<City> city_gd = new ArrayList<City>();
City c0 = new City("shi", "--市--");
City c1 = new City("guangzhou", "广州市");
City c2 = new City("shenzhen", "深圳市");
city_gd.add(c0);
city_gd.add(c1);
city_gd.add(c2);
JSONArray jsonArray = JSONArray.fromObject(city_gd);
String jsonStrCity = jsonArray.toString();
System.out.println(jsonStrCity);
out.println(jsonStrCity);
} else if ("jx".equals(sheng)) {
List<City> city_js = new ArrayList<City>();
City c0 = new City("shi", "--市--");
City c1 = new City("nanchang", "南昌市");
City c3 = new City("yichun", "宜春市");
city_js.add(c0);
city_js.add(c1);
city_js.add(c3);
JSONArray jsonArray = JSONArray.fromObject(city_js);
String jsonStrCity = jsonArray.toString();
System.out.println(jsonStrCity);
out.println(jsonStrCity);
} else if ("s".equals(sheng)) {
List<City> city_js = new ArrayList<City>();
City c1 = new City("shi", "--市--");
city_js.add(c1);
JSONArray jsonArray = JSONArray.fromObject(city_js);
String jsonStrCity = jsonArray.toString();
System.out.println(jsonStrCity);
out.println(jsonStrCity);
}
}else if("/getCountry".equals(path)){
String shi = request.getParameter("shi");
System.out.println("shi:"+shi);
if("guangzhou".equals(shi)){
List<Country> list = new ArrayList<Country>();
list.add(new Country("qu/xian","--区/县--"));
list.add(new Country("haizhuqu","海珠区"));
list.add(new Country("tianhequ","天河区"));
list.add(new Country("yuexiuqu","越秀区"));
list.add(new Country("huaduqu","花都区"));
JSONArray jsonArray = JSONArray.fromObject(list);
String jsonStr = jsonArray.toString();
out.println(jsonStr);
}else if("nanchang".equals(shi)){
List<Country> list = new ArrayList<Country>();
list.add(new Country("qu/xian","--区/县--"));
list.add(new Country("haizhuqu","一区"));
list.add(new Country("tianhequ","二区"));
list.add(new Country("yuexiuqu","三区"));
list.add(new Country("huaduqu","四区"));
JSONArray jsonArray = JSONArray.fromObject(list);
String jsonStr = jsonArray.toString();
out.println(jsonStr);
}else if("shenzhen".equals(shi)){
List<Country> list = new ArrayList<Country>();
list.add(new Country("qu/xian","--区/县--"));
list.add(new Country("haizhuqu","1区"));
list.add(new Country("tianhequ","2区"));
list.add(new Country("yuexiuqu","3区"));
list.add(new Country("huaduqu","4区"));
JSONArray jsonArray = JSONArray.fromObject(list);
String jsonStr = jsonArray.toString();
out.println(jsonStr);
}else if("yichun".equals(shi)){
List<Country> list = new ArrayList<Country>();
list.add(new Country("qu/xian","--区/县--"));
list.add(new Country("haizhuqu","奉新县"));
list.add(new Country("tianhequ","高安市"));
list.add(new Country("yuexiuqu","万载县"));
list.add(new Country("huaduqu","新建县"));
JSONArray jsonArray = JSONArray.fromObject(list);
String jsonStr = jsonArray.toString();
out.println(jsonStr);
}else if("shi".equals(shi)){
List<Country> list = new ArrayList<Country>();
list.add(new Country("qu/xian","--区/县--"));
JSONArray jsonArray = JSONArray.fromObject(list);
String jsonStr = jsonArray.toString();
out.println(jsonStr);
}
}
}
}
- 1
- 2
- 3
- 4
前往页