对标上一个随笔,jsp返回数据servlet一直接收不到,最后狠一点直接用流了,中间涉及了很多的转换,以及ContentType的设置都很关键
ContentType;
推荐园友的博客:介绍的比较详细:https://www.cnblogs.com/tugenhua0707/p/8975121.html
需要使用阿里巴巴的fastjson
servlet流的获取:
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
// 1.用流的方式将数据从jsp获取 +各种数据类型的转换
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8")); // 将数据使用流进行传递
StringBuffer strb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) { // 遍历数据
strb = strb.append(line); // 数据暂存StringBuffer
}
//转成JSONObject
JSONObject obj = (JSONObject) JSONObject.parse(strb.toString());
// 2.JSONObject转String
String value = obj.toJSONString();
System.out.println("inputvalue" + value);
value = "[" + value + "]";
//3String转listmap
// 数据转换为list
List<Object> getlist = JSON.parseArray(value);
// 转换listmap使用,最后都存入了这里
List<Map<String, Object>> getlistvalue = new ArrayList<Map<String, Object>>();
for (int i = 0; i < getlist.size(); i++) {
// Map<String,Object> ageMap2 = new HashMap<String,Object>();
System.out.println("获取的jsp值" + getlist.get(i));
Map<String, Object> ret2 = (Map<String, Object>) getlist.get(i);// 取出list里面的值转为map
// 将获取的值成功存入listmap中
getlistvalue.add(ret2);
}