以json格式接收集合

前端代码
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%
String basePath = request.getScheme() + “😕/” + request.getServerName() + “:” + request.getServerPort()
+ request.getContextPath() + “/”;
%>

Insert title here
<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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值