servlet中常用到的工具

时间:2021-09-29 19:11:11

1. 解析ajax传来的json字符串,得到json对象

 1 private JSONObject getJsonObject(HttpServletRequest req) {
 2         StringBuffer json=new StringBuffer();
 3         String lineString=null;
 4         BufferedReader reader;
 5         JSONObject data=null;
 6         try {
 7             reader = req.getReader();
 8             while((lineString=reader.readLine())!=null) {
 9                 json.append(lineString);
10             }
11              data=JSONObject.fromObject(json.toString());
12         } catch (IOException e) {
13             e.printStackTrace();
14         }
15         return data;
16     }