前端代码
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%
String basePath = request.getScheme() + “😕/” + request.getServerName() + “:” + request.getServerPort()
+ request.getContextPath() + “/”;
%>
<button id="djBtn">点击</button>
<br />
<br />
<table align="center" width="70%" border="1" cellpadding="6" cellspacing="0">
<thead>
<tr>
<td>
序号
</td>
<td>
编号
</td>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
操作
</td>
</tr>
</thead>
<tbody id="tBody">
</tbody>
</table>
后台代码
package com.wkcto.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 com.wkcto.domain.Student;
public class MyServlet7 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入servlet7");
Student s1 = new Student("A0001","zs",23);
Student s2 = new Student("A0002","ls",24);
Student s3 = new Student("A0003","ww",25);
Student s4 = new Student("A0004","zl",26);
Student s5 = new Student("A0005","sq",27);
List<Student> sList = new ArrayList<Student>();
sList.add(s1);
sList.add(s2);
sList.add(s3);
sList.add(s4);
sList.add(s5);
//{"id":"?","name":"?","age":?}
//{"sList":[{"id":"?","name":"?","age":?},{},{},{},{}]}
StringBuffer buf = new StringBuffer();
buf.append("{\"sList\":[");
for(int i=0;i<sList.size();i++){
//取得每一个学生
Student s = sList.get(i);
//buf.append("{\"id\":\""+s.getId()+"\",\"name\":\""+s.getName()+"\",\"age\":"+s.getAge()+"}");
buf.append("{\"id\":\"");
buf.append(s.getId());
buf.append("\",\"name\":\"");
buf.append(s.getName());
buf.append("\",\"age\":");
buf.append(s.getAge());
buf.append("}");
if(i<sList.size()-1){
buf.append(",");
}
}
buf.append("]}");
String str = buf.toString();
System.out.println(str);
//为ajax 响应数据
PrintWriter out = response.getWriter();
out.print(str);
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}