Servlet 接口

public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{

String username = req.getParameter("username");//用户名

String password = req.getParameter("password");//密码

if(username.equals("111")&&password.equals("111")){

JSONObject datas = new JSONObject();

JSONArray rs = new JSONArray();

JSONObject obj = new JSONObject();

obj.put("uid", "34860");

obj.put("uname", "411");

obj.put("utype", "5");

obj.put("nickname", "abcdef");

obj.put("myphoto", "11111111111111111111111");

rs.add(obj);

datas.put("result", "1");

datas.put("userinfo",rs);

String info = datas.toString();

System.out.println(info);

req.setAttribute("info", info);

req.getRequestDispatcher("text.jsp").forward(req,resp);

}else{

 

}

 

}

 

//---------------------------------------------------------------------------------------------------

String httpStr = "https://api.douban.com/v2/book/1220562";

String theWord = null;

URL url = new URL(httpStr);

InputStream ins = url.openConnection().getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(ins,"utf-8"),2048);

theWord = br.readLine();

System.out.println(theWord);

JSONObject jsonArray = JSONObject.fromObject(theWord);

List list = new ArrayList();

String name = jsonArray.getString("rating"); 

System.out.println(name);

String publisher = jsonArray.getString("publisher"); 

System.out.println(publisher);

 

String author = jsonArray.getString("author");

System.out.println(author);

String pubdate = jsonArray.getString("pubdate");

System.out.println(pubdate);

String price = jsonArray.getString("price");

System.out.println(price);

 

for (int i = 0; i < jsonArray.size(); i++) {

  Object o = jsonArray.get(i);

  JSONObject jsonObject = JSONObject.fromObject(o);

  Shop Person = (Shop) JSONObject.toBean(jsonObject, Shop.class);

  list.add(Person);

 // list.add(Person);

}

 

 

//--------------------------------------------------------------------

List list = new ArrayList();

 

JSONArray arr = jsonArray.getJSONArray("books");

    for(int i=0;i<arr.size();i++){

    JSONObject jb = (JSONObject)arr.get(i);

    Shop person = new Shop();

    person.setAuthor(jb.get("author").toString());

    person.setImage(jb.get("image").toString());

    person.setTitle(jb.get("title").toString());

    list.add(person);

    }

   

    request.setAttribute("list", list);

    request.getRequestDispatcher("/book_list.jsp").forward(request, response);

 

你可能感兴趣的:(servlet)