联动

1.引入js

2.联动动态

动态联动下拉列表

查询条件:
年龄: 满足条件的学生:

 --------------------------------------------------------------

 public class DynamicList extends HttpServlet {

private ArrayList students = new ArrayList();

 // 定义Student内部类

public static class Student {

private int id;

 private String name;

private int age;

 private String gender;

private String date;

public Student(int id, String name, int age, String gender, String date) {

 super(); this.id = id; this.name = name; this.age = age; this.gender = gender; this.date = date;

} }

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 // 设置生成文件的类型和编码格式

response.setContentType("text/xml;charset=UTF-8"); // 设置响应内容格式

 response.setHeader("Cache-Control", "no-cache");

PrintWriter out = response.getWriter();

String output = ""; // 处理接收到的参数

 int age = 0;

 if (request.getParameter("age") != null) { // 如果age属性不为空

age = Integer.parseInt(request.getParameter("age")); // 得到age的值

}

ArrayList studentList = new ArrayList();// 创建student集合对象

 if (age == 0) studentList = students; else studentList = getStudents(age);

// 生成响应文档

 if (!studentList.isEmpty()) {

 output += ""; for (int i = 0; i < studentList.size(); i++) { // 遍历对象集合

Student stu = studentList.get(i); // 取得对象

 output += "";

output += "" + stu.id + "";

output += "" + stu.name + "";

output += "";

}

output += "";

 }

out.print(output); // 向客户端传递信息

out.close();

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 this.doGet(request, response); } // 初始化数据集合

 public void init() throws ServletException {

 students.add(new Student(1, "Marlin", 22, "Gril", "2007-8-5"));

 students.add(new Student(2, "Tom", 22, "Boy", "2007-8-7"));

 students.add(new Student(3, "Jack", 22, "Boy", "2007-8-3"));

students.add(new Student(4, "Mac", 23, "Boy", "2007-8-7"));

students.add(new Student(5, "Mercy", 23, "Girl", "2007-8-9"));

students.add(new Student(6, "Rain", 23, "Boy", "2007-8-10"));

 students.add(new Student(7, "Luice", 24, "Girl", "2007-8-10"));

students.add(new Student(8, "Jonse", 24, "Boy", "2007-8-11"));

 students.add(new Student(9, "Lily", 24, "Girl", "2007-8-12"));

}

 // 选出所有满足条件的学生

public ArrayList getStudents(int age) {

ArrayList result = new ArrayList(); // 创建student对象集合

 if (!students.isEmpty()) { // 如果集合对象不为空

 for (int i = 0; i < students.size(); i++) { // 遍历对象集合

Student student = students.get(i); // 取得对象

 if (student.age == age) {

result.add(student);

} } }

return result;

} }

 

posted @ 2012-03-21 21:54  残星  阅读(299)  评论(0编辑  收藏  举报